In Godot 4, the setget
keyword is gone and has been replaced by using properties, so you don’t have to use dedicated functions. We will go through some examples of how to use getters and setters.
Getting ready
For this recipe, create a new scene by clicking + to the right of the current Scene tab and add Node2D. Click on the word Scene in the top menu next to Project, then select Save Scene As, and name it Properties
.
How to do it…
There are two ways we can use getters and setters. The first is like Godot 3.x, where we assign get
and set
to functions. The second way is to define get
and set
after we declare the variable. First, let’s create a button:
- In the new scene named Properties that you created, add a Button node and make it big enough to see.
Figure 2.2 – Creating a Button node
- Add a script named
Properties.gd
to the Button node and delete everything except for line 1.
- At the top center of the editor, left-click on 2D in the Workspace section. Add a signal to the Button node by going to the Node tab to the right of the Inspector tab located under BaseButton and selecting pressed():
1 extends Button
2
3
- The
setget
keyword used in Godot 3.x is now gone. We will assign get
and set
to functions as was done in Godot 3.x:5 var value: int = 10: set = set_value, get = get_value
6
7 func set_value(new_value: int) -> void:
8 value = new_value
9 print('setter', str(value))
10
11 func get_value() -> int:
12 print('getter', str(value))
13 return value
14
15 func _on_pressed():
16 value -= 1
- Now click the Run the current scene button or hit the F6 key. Look at the Output section in the bottom panel.
- Highlight lines 5–13 and hit Ctrl + K to comment out these lines.
- Now we will try using a variable declaration and no functions. Let’s start on line 15. Lines 23 and 24 should still be there from step 4. They were lines 15 and 16:
15 var value: int = 10:
16 set(new_value):
17 value = new_value
18 print('setter', str(value))
19 get:
20 print('getter', str(value))
21 return value
22
23 func _on_pressed():
24 value -= 1
- Now click the Run the current scene button or hit the F6 key.
How it works…
We added a button and connected the pressed()
signal so we could see that the getters and setters were working.
In lines 7–9, we created the set_value
setter function with an integer parameter called new_value
. This function does not return a value so we used -> void:
. We then made value
equal to new_value
, and finally, we printed setter
with value
.
In lines 11–13, we created the get_value
getter function, which returns an integer, so we used -> int:
. We printed getter
with value
and then we returned value
. In line 16, we added value = value – 1
to the _on_pressed()
function that was created when we hooked up the signal in step 3.
We entered the code to call the get
and set
functions as was done using the getset
keyword in Godot 3.x. In line 5, we created a variable called value
, which is an int
value equal to 10
. We then assigned set
to the set_value
function and get
to the get_value
function.
Figure 2.3 – The GDScript and console output results
When the button was clicked, the value
variable in the setter was decreased by one and then printed on the console, as well as the value
getter variable before and after the button was pushed, to show that the getter and setter were working.
We commented out lines 5–13 so we could use the get
and set
properties with the variable declaration.
On line 15, we created an integer variable called value
and assigned it to equal 10
. Then, on line 16, we used the set
property with new_value
as the parameter. On lines 17–18, we made value
equal to new_value
and printed setter
with value
. On line 19, we saw the get
property. On lines 20–21, inside of the get
property, we printed getter
with value
.
Figure 2.4 – The GDScript and console output results
We select Run the current scene and notice that we get the same output on the console as we did when we used functions. The value
variable in the setter is decreased by one and then printed on the console, as well as the value
getter variable before and after the button was pushed.