GODOT GUIDE – PART 2 – INTRODUCING SCRIPTS

By Alex K.

 

 

After completing the first guide, you can’t do anything but stare at the face, because we don’t have any scripts.

Scripts are the instructions that the game uses to run.  In this guide, we will learn some of the basics regarding writing scripts in Godot.

 

1.     Close off your game (if it is still running in front of you).

 

2.      At the top left, right-click on Icon in the Scene Tree, and press Attach Script…

 



3.     A window titled Attach Node Script should pop up like below.

 

https://lh7-rt.googleusercontent.com/docsz/AD_4nXcClftjsXxo6VdMbaW9DCQLU3G0S8oKdQR1hhSeKCWVaZfG-Hwrri9MVQDScJz6aI-eR2DNQLvlWBjL7IvdwC7n2Ywcyh5mhm3-Dt2oQ7KreWh_YGQMJWmBrh1X1FMdaKZ59zgwFQ?key=A319pMMFgwhCL9UM9V1AOWMl

 

4.     Change the path to reflect what the code will be affecting.  We’ll call it player.  But make sure to leave the “res://” and the “.gd” in place.  Like below:

 



5.     Hit Create.  You are automatically brought to the Script tab and the default script code is showing.

 

https://lh7-rt.googleusercontent.com/docsz/AD_4nXeI5l_NSlPC0MqgEGmsUwKvkn7KJ6fMxKLf-V4_GsYUVVtGmJ9TwDslbt-bYm9uQbmedjqurY7soxyvTblBxVFPwBMrFxls5n8t7zMvxHOSgTDwYrnX-EZlT7MeYjEEjxS88NWQvw?key=A319pMMFgwhCL9UM9V1AOWMl

 

6.     Before getting into the code, take a minute to switch between the 2D Scene tab at the very top middle and the Script tab.

 

7.     Back to the default script code, you should notice three things:

 

·       The extends Sprite2D means that this script can give orders to Sprite2D nodes. For example, it could “change your image” or “play an animation”.

·       Everything indented after _ready() will run a single time when you start the game.  This is known at the _ready function.

·       Everything indented after _process(delta: float) will run repeatedly over and over.  This is known as the _process function.


https://lh7-rt.googleusercontent.com/docsz/AD_4nXcr0pfMNxZOMhUs-qMCavhtQ8XARGkZHQXP7KEv7LOHGLUfVEEsLwXo-HANi2GSCxNnajpQbYBaC60ygw_4MafYen62njOWC-6RzIteJaZ4WBvm84fKK6p-GkMWrp3wfvoMNuQ9?key=A319pMMFgwhCL9UM9V1AOWMl

 

 

8.     Let's start coding. Start by removing the pass from after _ready.

9.     Then add the following line (making sure it is indented):

     print("hello world!!!!")

 

 

 

10.  Now run your game.  Look at the console (little box below the scripts). You should see the message “Hello world” appear.  Pretty cool right?

11.  Optional:  What do you think happens if you were to place the print(“hello world!!!!”) line under the _process function instead?  Try it.