Guide to the Godot game engine/Programming


 * GDScript

The 3 languages
Godot has 3 main programming (or scripting) languages, and a bonus fourth one if you have a version of Godot with Mono compiled. However, you can get unofficial languages or even make your own!

GDScript
GDScript (".gd" file extension) is Godot's main language. It is a custom language that works with Godot's "scene" system. It is also quite simple to use. The developers of Godot say that you could learn GDScript in under an hour.

Example "Hello world!" program: -> :

I recommend using GDScript if you've never used Godot before since it is the easiest to use and also sports an auto-completion, which is very useful. GDScript is the language that will be used throughout this book.



Fun fact: Godot's developers experimented with Python and Swift before deciding to make a custom scripting language that better fit Godot. As a result, the language is pretty similar to the two.

For more information, please see the GDScript chapter.

VisualScript
VisualScript (".vs" file extension) is Godot's visual language that uses a user-friendly interface to make code. It lacks many features in GDScript, but can be used to create, for example, a conversation tree.



However, this language is discontinued for Godot 4.x, as only 0.5% of users actually used the language. It is still available in Godot 3.x, and may be added to Godot 4 as an extension at a later date.

GDExtension (NativeScript before Godot 4.x)
GDExtension allows you to run c and c++ code without recompiling it with the engine. This allows you to create super-fast code for things that are very computationally expensive.

GDExtension in Godot 4.1+ is forwards compatible, meaning GDExtensions made for 4.2 will work in 4.3, but not vice versa. However, this is not a guarantee since in the future compatibility might break in order to fix a bug and add a critical feature.

Unlike engine modules, GDExtension doesn't require compiling the engine's source code. It gives you access to most of the API available to GDScript and C#, allowing you to code game logic with full control with performance. It's ideal if you need fast code you'd like to distribute as an add-on in the asset library.

GDExtension can also use other languages, you're not limited to c and c++. If you want to use other languages you will need to install community-made bindings. They can be found on the official documentation pages.

C#
If you have the mono version of Godot, you can write C# code. You can use it to make high-speed code to make modules or objects for your game. It can also be used to write tools.

How do I use these?
GDScript, VisualScript and GDExtension (or NativeScript) can all be attached to a node, or used to create a custom one. In the mono build of Godot, C# can also be attached. C++ can only be used in Godot's existing engine. To do so, you will need to download a special version of Godot that has not been compiled into an application file.

To attach a script to a node, you first need a node to attach it too! Assuming you read the last chapter ("What is a node"), you can begin from the project you started last time.

Open Godot, and open your test project by double-clicking on it in the menu at the center of the screen. Open "Test UI.tscn" from the File System dock if it is not already open. Select your Test UI Control node. At the top of the Scene dock is a button that looks a little like paper with the edges of two sides rolled up. Press it.

Click the "Template" button and select "Empty" from the drop-down menu that appears. Press Create. Now a new screen should appear with a text edit area in the center of the screen, and the Test UI node should have that paper icon on it. You can click it whenever you want to reopen the script here. The script looks pretty boring right now:

This is very important, however! The  keyword (a very important piece of code, like  ) tells Godot that your script inherits the node type you place here. You may only attach a script to a node of that type, or a node that inherits that type.

Under, write the following code:

-> :

tells Godot that you want to define a function. If you use Python a lot, you might think the  never gets called, as it is inside a function, which never get called automatically. The built-in function  tells Godot to run the code when the node with the script is "ready", or that it has been fully loaded.

runs before the node is ready. Use it to do self-contained pre-construction. runs every frame as fast as possible. is an argument for the function. It represents the time in seconds since the last frame. The value rises as the game lags. is similar to, it runs every physics frame on a fixed time step, which is typically 60 frames per second. is ran when the node exits the scene (to become an orphan or when it is "freed", or deleted) or the game closes.

Now, for some more complex code. This program will record the time that has passed in seconds since the game ran and show it when it ends.

time: = (delta): time += delta :  (,time)

Now, lets update this to automatically close the game after a random amount of time.

Add this code before :

MAX_TIME = MIN_TIME = time_to_quit= % (MAX_TIME-MIN_TIME) + MIN_TIME +

This will create a variable and set it during the call to  automatically. is a random number between 5 and 10. Add this code in the  function:

time >= time_to_quit: .

One problem... The number is never different. Why? Because you need to call  first.

Change the declaration of  to   and add this to the   function:

time_to_quit = % (MAX_TIME-MIN_TIME) + MIN_TIME +

The  is necessary since   returns a random number between   and.

This creates a number between 0 and  subtracted by   subtracted by 1. Adding the  and an extra 1 gets the result you need, a random number between   and  ..

The  makes   give a different stream of random numbers. Randomizing is based on time, and should not be called every frame for performance reasons. Once per node, called during  call, is enough.

What you have learned

 * 1) The 3 (4 for Godot 3.x versions) types of currently supported script
 * 2) How to attach a script to a node
 * 3) How to record time with delta
 * 4) How to close a game
 * 5) How to make random numbers.

Quiz
 {NativeScript is the code the engine uses and requires recompiling the engine to use. } - True + False
 * type=""
 * C++ is what the engine uses.

{What language was discontinued for Godot 4? } +Visual Script -GDScript -C++ -C# -Python
 * type=""

{GDScript is based on what other popular programming language(s)? { Python|Swift|python|swift|Python and Swift|python and swift|Python or Swift|python or Swift|Swift and Python|swift and python|Swift or Python|swift or python }
 * type="{}"}
 * Python and/or Swift. Both are valid.

{Which functions use delta as an argument? +_process +_physics_process -_ready -_exit_tree
 * type="[]"}

{Which of these process at a fixed time step? -_process +_physics_process -_second_process
 * type=""}

{The word "delta" is also a keyword: -True +False
 * type=""}
 * If it were, it wouldn't be an argument.

