Guide to the Godot game engine/Programming
The 3 languages Edit
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 Edit
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:
func _ready() -> void: # This is a comment. It isn't shown in the game. print("Hello world!")
It is the language I understand most, and will be used throughout the 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 Edit
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[1], 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.
NativeScript Edit
NativeScript (".dll" file extension) can be used to make super fast code and modules without recompiling the engine.
It has also been used to create unofficial support for more common languages like Python and Swift.
As I have no knowledge of NativeScript, this section will likely remain short. If you have knowledge of it, please put it here! Thanks!
C# Edit
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.
Note: Godot itself is made with C++, and making direct modifications require you to recompile Godot's editor. Many Godot users have used this method to make custom Godot editors with custom tools.
How do I use these? Edit
GDScript, VisualScript and NativeScript can all be attached to a node, or used to create 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:
extends Control
This is very important, however! The extends
keyword (a very important piece of code, like pass
) 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 extends Control
, write the following code:
func _ready() -> void: print("Hello world!")
func
tells Godot that you want to define a function. If you use Python a lot, you might think the print
never gets called, as it is inside a function, which never get called automatically. The built-in function _ready()
tells Godot to run the code when the node with the script is "ready", or that it has been fully loaded.
_init()
runs before the node is ready. Use it to do self-contained pre-construction.
_process(delta)
runs every frame as fast as possible. delta
is an argument for the function. It represents the time in seconds since the last frame. The value rises as the game lags.
_physics_process(delta)
is similar to _process
, it runs every physics frame on a fixed time step, which is typically 60 frames per second.
_exit_tree()
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.
var time: float = 0 # Or just "var time = .0" # The "-> void" is optional func _process(delta): # This increments time time += delta func _exit_tree(): print("Time passed: ",time)
Now, lets update this to automatically close the game after a random amount of time.
Add this code before _process
:
const MAX_TIME = 10 const MIN_TIME = 5 onready var time_to_quit=randi() % (MAX_TIME-MIN_TIME) + MIN_TIME + 1
This will create a variable and set it during the call to _ready
automatically. time_to_quit
is a random number between 5 and 10. Add this code in the _process
function:
if time >= time_to_quit: # Quit after a random amount of time. get_tree().quit()
One problem... The number is never different. Why? Because you need to call randomize()
first.
Change the declaration of time_to_quit
to var time_to_quit
and add this to the _ready
function:
randomize() time_to_quit = randi()% (MAX_TIME-MIN_TIME) + MIN_TIME + 1
The + 1
is necessary since randi()%X
returns a random number between 0
and {{Gdscript/string|X-1))
.
This creates a number between 0 and MAX_TIME
subtracted by MIN_TIME
subtracted by 1. Adding the MIN_TIME
and an extra 1 gets the result you need, a random number between MIN_TIME
and MAX_TIME
..
The randomize()
makes randi()
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 _ready
call, is enough.
What you have learned Edit
- The 3 (4 for Godot 3.x versions) types of currently supported script
- How to attach a script to a node
- How to record time with delta
- How to close a game
- How to make random numbers.
See also Edit
Quiz Edit
References Edit
- Getting started [ ]
- Making it work
- Making it look good
- Advanced help
- Miscellaneous
<-- previous
back to top
next -->