What we have done in the editor so far was just putting together the resources that we want to use in the game and organize them. But how do we make things happen? How does the player interact with the world? How do we calculate the score? All this will need code to happen.
For this purpose Godot uses its own scripting language GDScript which takes a lot from Python. It is made to be simple to learn, readable, and is delivered with a lot of predefined classes that fasten the game development. It doesn't need compilation and is dynamically typed. Of course, this is not made to speed up rendering but make it so easy to use that it's worth the choice. Most of the optimisation is done in the engine and GDScript gives a lot of performance improvement over Python in the game context. For those who need more control of the internal workings of their game, C++ can also be used.
To read this chapter, it is better to have some basic knowledge of some programming concepts. You may find courses about python in our manual.The main part of the code structure is given by Godot itself. Scripts will be attached to nodes and extend the classes those nodes are based on. We will just need to define what is specific to the game: mostly variables and functions.
Variables are explicitely declared:
Use the keywordvar to declare the variable. As GDScript is dynamically typed, there is no need to define if it is an integer or a string or any other type. This will be done when a value is given.
var speed
Of course you can affect a default value at the beginning:
var speed = 100
Or at any time in any function to which the variable is accessible:
speed = 100
GDScript comes up with standard types (like int, float, string, bool, null, array, dictionary…) but also has some specific types related to common maths calculation in a game (such as Rect2, Vector2, Vector3…) and some engine-specific types (such as Color, Image, Object, InputEvent…). It is not really the purpose of this manual to describe then intensively. Refer to Godot's in-editor reference for more information about each type.
Note that one can also define constants with theconst keyword instead ofvar.We will sometimes want to "export" a script variable. When doing so, the variable will be displayed in the inspector for the nodes that the script extends. This is a good practice coders should apply for important variables. It helps non-coders and game designers to test several values and improve the gameplay without risking to break all the code. We will give an example for that later on.
export var speed = 100
To declare a function you simply need to use the func keyword like this:
func function_name().If you had a look at Python, you will notice that there is no def, and no self which is considered as implied. On the other hand, conditions, while or for loops are really similar.
# Function magic goes here
Predefined Godot functions begin with an underscore. For example the default _ready() pre-inserted in any new script as we will soon see.
func _ready():
# Initialization goes here
Of course, as our script extends the base class, each function is a method of a class and might be called as a member.
var player = get_node("player")
player.do_this()
Enough words for now, let's see the process of adding a script.
extends Node2D
(that means that all variables and methods of Node2D are available to this script) and a _ready()
function that we'll customize. The Inspector displays resource properties like path and name._ready
function, deletepass and add instead print("test")
. Save the script and play the game or the scene. This will play the game but also display the Debugger and the Output panel. If everything is fine, "test" should appear in the output panel._ready
function. So we get this:func score():
# Initialization goes here
print("In score function") # Check that everything works
func _ready():
print("Test")
score()
if (get_node("Label")):
print("Label found")
else:
print("No label node")
If a node named Label is found, the first message is displayed. Which is good. On the other way, change the label name to match your need. Finally, we need to know how to set the text in the game. If you have no idea of what to use just go to the Helptab, in the Search in Classes column, look for Label. Click on it to display all the method available for that class. On top we have a Public Methods, especially on called set_text(), which looks appropriate. Click on it to display details which say simply «set the label text». It could be worse. Let's use it and play:
if (get_node("Label")):
get_node("Label").set_text("0")
else:
print("No label node")
var obstacles = []
var spawn_ofs = 1000
export var spawn_distance = 500
func spawn_obstacle():
var obs = preload("res://pipes_random.scn").instance()
obs.set_pos(Vector2(spawn_ofs, 0))
spawn_ofs += spawn_distance
add_child(obs)
print("Obstacle spawned at ", obs.get_pos())
First we declare our variables: obstacles, spawn_ofs (initial offset) and spawn_distance. See that we added export keyword before the third variable. This will make it displayed in the Inspector to let the game or level designer play with the value and equilibrate the game, without having to open the script file.
Then we added a function which preloads the scene containing the buildings and instantiates them. We set the position of the new instance in a Vector2 using a variable x value. We then update values for the next instance and add the instance as a child of the current node.
Our function will be used as the bat will move. So that is now what we have to do now.
// show autoload
Il y a une erreur de communication avec le serveur Booktype. Nous ne savons pas actuellement où est le problème.
Vous devriez rafraîchir la page.