📜 Scripting | Betabox

📜 Scripting

In Godot, we will primarily use GDScript to write our scripts. This text-based programming language is simple to use and will have our games up and running in no time! If you want a sneak peek at GDScript, check out the cheat sheet that came with your box!

Scripting a Scene!

Let’s get started by scripting a simple scene. This scene will consist of a label and a button. Clicking the button will cause the label to change the text! Before we get started we need to create a new project! Once we have created the project, we can start adding nodes!

We first want to add a panel node. This node will give us the background to place our button and label. After we have added the panel node, we will need to add the button node and the label node as children!

After you have added all the nodes, your scene tree should look something like the above!

Now we can use the 2D editor to expand our panel and position our button and label. You want to try to make it look something like what is below!

If you are having trouble setting the text for either of the nodes, remember that that is done in the Inspector pane after you have selected the node!

Now we want to attach a script to our panel node. To do this, we right-click the node and click Attach Script. You will see a pop-up window that looks something like the following:

The only thing we might want to change in this window is the Path name. Once you have done that you can click create and you will see the scripting workspace open! You will notice that there is already some code written in the workspace. This is to make development that much easier. Thanks, Godot!

Handling a Signal!

Signals are sent when an event happens in Godot. These signals are sent to tell another node to do something. Let’s do an example to help you see what I mean! Normally, we will create these signals in scripts but for now, we will do it directly in the editor.

First, you will need to select the button node in the scene tree and click on the node tab in the Inspector pane. Make sure that Signals is selected as in the following picture:

Once you have that, simply double click on pressed() to open up the Connect window which should look something like this:

Once you click connect, the signal will be set up!

That is one way to set up a signal but let’s talk about how to do the same thing in a script!

Inside the script that we created earlier, we are going to add a few lines. The first thing to add is a line of code that will allow us to get the node we want to send the signal. The function we use to do this is called get_node(). To use this function correctly, we will add it to the _ready() function in our script so that it looks something like this:

You can see here that we use the get_node(“Button”) to get the appropriate node from our scene! Then, we use the connect() function to declare what type of signal it is!

Next, we will need to code what happens when the button is pressed. To do this, we will add code to the _on_Button_pressed() function. This code should look like this:

Once again, we will be using the get_node() to get the label node and change its text! This will cause the button to change the text to “HELLO!” when you first click it.

Now you can click the run scene button and see the product of your work! You should be able to press the button and see this outcome: