Subscribe to GameFromScratch on YouTube Support GameFromScratch on Patreon


22. January 2015

 

Welcome to the first coding tutorial of the GameFromScratch.com Godot tutorial series.  We are going to take a quick look at the Godot IDE then jump in and create our first application.  I want to say right up front, I am no Godot expert, so if I may a mistake or you spot something questionable be sure to point it out!  There certainly may be a better or more efficient way of doing things.  There is also a video version of this tutorial available here.  An embedded version is available down below.  The video covers the same topic but may go into a bit more detail.  This first tutorial goes in to a great deal of detail and all subsequent tutorials will assume you’ve read or watched the ones that come before.  As a result, future tutorials will probably be much shorter, or at least, have less screenshots.

 

Alright, without further ado, let’s jump in!

 

First off, you obviously need to have Godot installed before you continue.  If you havent already, go download Godot now.

 

Creating an initial project

 

Now when you run Godot you should see the following window appear:

image

 

If you have an existing project, you can load it here.  Also, if you have downloaded the demos and samples ( direct zip link ) you can load them using the Import button.  In our case however we want to create a new project.  Click the New Project button:

image

 

A new dialog will open.

image

 

Click the Browse button and navigate to the folder you wish to create your project in.  Keep in mind, Godot will not create a folder for your project, so use the Create Folder button should you need one.  Here are the settings I am using:

image

 

Once you have selected your directory, click Open.  Then in the previous dialog, the project name will be set to that of the directory you choose.  Once happy, click Create.

image

 

Now either double click or click select your project and choose Edit

image

 

 

Adding A Node to the World

 

You should now be in the main editor interface.  Let’s start creating our application.  First thing we need to add a text node to our world.  On the right hand side of the screen, make sure the Scene tab is selected, then click the Add/Create a new node button.

image

 

Now in the resulting dialog, locate RichTextLabel then click Create.

image

 

A new node will be added to your scene hierarchy:

image

 

You can rename the node by double clicking, I’ve renamed mine HelloWorldLabel:

image

 

With the Node selected, you can see it’s editable properties in the Inspector window:

image

 

Adding a Script

 

One thing you might notice is there is no property available to set the text!  Time for a bit of scripting.  We need to attach a script to our node.  In the scene panel, select your RichTextLabel node and click the script icon:

image

 

If there isn’t a script attached, this icon will bring up the following dialog:

image

 

Click the .. next to Path.  Now click “Create Folder”, create one called Scripts then click OK.  I should point out, this is completely optional, you could of course just put all the stuff in the root of your application.  You should note that the file system in Godot is entirely relative to the projects root directory.  If you want an asset included in your game, it needs to be copied into this folder or a subfolder to be usable.

image

 

Now name your script.  NOTE!  You have to add the .gd file format, unfortunately Godot wont do it for you.  I called mine “MyButtonScript.gd”.  Once done, click “Save”

image

 

Finally click “Create”

image

 

Script Editing

 

Once you hit Create, you should automatically be brought to the code editing window, like so:

image

 

As you can see, Godot has created a new script that inherits from RichTextLabel.  It implements the _ready() method which is where you do all the creation related logic in your script.  If you come from another language, _ready is not the constructor, it is not the first method to be called.  If you need constructor like behavior, instead use _init().  _ready() is an overrideable function inherited from Node which is the base class of all things that can appear in the Scene.  You will be seeing a lot of Node in the future!

 

As to GDScript itself, it’s a Python-esque scripting language, so if you’ve got some Python experience, you will feel immediately at home.  If you are used to curly brace ( { } ) languages like C++ or Java, the critical thing to understand is it uses newlines in place of curly braces and semi colons.  Don’t worry, while jarring at first, you will quickly get used to it.

 

Now if you want to brush up on GDScript, I recommend the following sources:

 

Additionally, you can access the library reference at any time in the Help tab or by Pressing F1:

image

 

You can also get context sensitive help by pressing Shift + F1 when using the Script tab.

 

Ok… that’s enough language overview, let’s change our code to set the text when our control loads.  Simply replace the code with:

extends RichTextLabel


func _ready():
   self.add_text("Hello World")

 

self is basically the same as this in other languages, which returns a reference to the local object.  In this case, self is a RichTextLabel.  We are calling the method add_text() that we inherited from RichTextLabel, setting the text for our control.

 

Next we want to run our code… but we can’t just yet.  First we need to set our default scene…  oh, and while we are on that topic, we need to save our scene!

 

Setting the Default Scene

 

First let’s save our scene.  Select the Scene menu button and select Save Scene.

image

 

In the resulting dialog, select a location and name.  Be sure to add the scn extension, as Godot requires it and wont do it for you!  Just like scripts, you could save all of your scenes in a scene folder, but in this case I’m just going to save in the root directory using the default name new_scene.scn.  When done click Save.

image

 

Ok, now that we have a scene, we need to set it as the default scene that is run when we load our game.  To do so, click Scene->Project Settings:

image

 

In the resulting dialog, locate main scene, then hit the folder icon to it’s right:

image

 

Select File…

image

 

Select your scene, then click Open.

image

 

Running your application

 

Now you can run your scene.  Simply click the play icon across the top of the application:

image

 

If everything went according to plan, you should see something like this:

image

 

Congratulations, you’ve just created your first Godot game!  Now, let’s make it suck slightly less!

 

To stop your application from running, simply click the Stop icon to the right of the play icon.

 

Editing a Scene Node

 

You may notice our text is across two lines and in the top left corner of the screen.  This is because we didn’t set the size or position.  Both of which can be done in the 2D Scene pane.

 

 

First, switch over to 2D Scene:

image

You can now see our RichTextLabel as a small rectangle at the top left corner:

image

You can use this window to position your various nodes in your scene.  If your node isn’t selected, you can select it by clicking it in the 2D window, or by selecting it from the Scenes panel to your right. 

 

Left click and drag within the selection to move it.  Grab one of the circles from the corner to resize it, like so:

image

 

If you look at the Inspector panel, you will see the values have been updated as you edit them in the scene:

image

You can also enter numeric values in the Inspector window if you prefer more precise control.  Just be sure to hit enter or your changes will not be saved.

 

Loading and changing a Font

 

One question you may have is, how do I change the size of the text?  The answer is, using a font.  You need to create a font for each size of text you want.  Fortunately creating fonts is incredibly simple.  From the Import menu, select Font

image

 

Now you will be able to import an existing font.  If you are running windows, several ttf ( TrueType Font ) files are located in Windows\Fonts.  Please note, these fonts are almost all trademarked and cannot be used without permission in a shipped game!  There are hundreds of open license TTF fonts available for download however.

image

So, open your font, select the size and properties you want to set, then set the Dest Resource filename, making sure to have the .fnt extension.  Finally click Import.

 

Now you need to set your RichTextLabel to use your new font.  With your label selected, in the Inspector window, locate Custom Font, click the drop down and select Load.  Select your newly imported font.

image

 

Now when you run the code, you should see:

image

 

Adding a Timer and wiring events

 

Now let’s add a bit of functionality to our app.  A lot of the logic in Godot is available in included nodes.  Let’s add a timer as a child to our RichTextLabel, that will be called every second, updating a counter on our message.

 

First we need to add a child node.  With our RichTextLabel selected in the Scene panel, click the New Node icon and add a Timer:

image

 

Select Timer and click Create.  Your timer should now appear as a child of your RichTextNode, like so:

image

 

Make sure the Timer is select and in Inspector, set the property AutoStart to on:

image

 

This will create a timer that is called every second and starts running as soon as it’s created.  Now lets connect the timer to our RichTextLabel.  In the Scene panel, with Timer selected, click the Edit Node Connections icon:

image

 

Next select the timeout() connection, which is the callback that will be called each time the timer “ticks”.  Then hit “Connect…”

image

 

In the next dialog, the defaults should be good.  This is wiring a connection between the timer object and the RichTextLabel.  When the connection is fired, it will call the method you are creating, in this case _on_Timer_2_timeout.  When done, click Connect:

image

 

This will automatically add a function to your script file and bring you to the Script editor.  Now let’s change our code slightly.  We are going to add a member variable that counts the number of seconds and update our total each time the timer ticks.  Edit your code like so:

extends RichTextLabel


var count = 0

func _ready():
   self.add_text("Hello World")

func _on_Timer_2_timeout():
   count += 1
   self.clear()
   self.add_text(str("Hello World!  Elapsed seconds = ",count))   

 

As you can see, we add a variable named count with the value 0.  GDScript is “duck typed” in that it infers the type from the value it is given.  So “if it looks like a duck, acts like a duck and quacks like a duck… it’s a duck!”, just applied to variables. 

 

Next you will notice Godot has added the func _on_Timer_2_timeout, which will be called by our timer each time it ticks.  We simply increment our counter, clear the existing value from out label, then add new text.  To concat our string and count variables together we use the built in str() function, which is similar to printf in other languages.

 

Now when your code runs, you will see:

image

 

… and that is the basics of creating an application in Godot.  In the future we will hopefully go in depth on all aspects of using Godot.  If you have any questions or comments, please don’t hesitate to ask!

 

The Video

 

Programming , ,

blog comments powered by Disqus

Month List

Popular Comments