Guide to the Godot game engine/Signals and methods
Many Godot users will say to you "signal up, call down" if you ask for tips with coding. Just what does this mean?
Signals
editSelect a node from the Scene dock. Press the "Node" tab next to the inspector. A list will appear. These are signals.
Create a new scene. Press User Interface. Save it as Signal test.tscn
. Add two nodes: a Button and a Label, both as children of the Control. Use the Layout
button to center the Button to the screen. Set the Text
property to Press Me
in the inspector.
Now add a script to the Control root node (the first one). Select the Button. Open the Node dock, and double click "button_up()". A popup will appear with your scene root (the Control node) selected. A text edit should say _on_Button_button_up
. Press "Connect".
This will create the code
func _on_Button_button_up(): pass # Replace with function body.
This function will be called when the signal button_up
is emitted. Add the code print("You pressed me!")
to the function. You may remove the pass # Replace with function body.
if you wish.
Run the scene by pressing F6, and grant that button's wish!
You can make your own signals with signal <signal_name> <optional:(argument1: type, argument2: type, ect)>
in GDScript. To emit it in a function: emit_signal(<"signal_name">, <argument1>,<argument2 ect.>)
.
Calling
editJust as important as signals, calling allows many things to happen. Add $Label.text = "You pressed me!"
to the function. This is actually setting a variable, but it's still considered calling, as setting a variable uses a setter function. More on those in another chapter.
The $path
is a short hand for the get_node(path)
function. The Label
is the path argument. This allows direct manipulation of the child node of that name.
These are called NodePaths. They work a bit like file paths, and this is a common example: $weapon/art.texture = preload("res://assets/weapons/upgraded_sword.png")
, and can also be relative, like (called in res://game/player.gd
) /weapons/upgraded_sword.tscn
.
A global get_node
looks like /root/game/player/weapon/art
.
For more, please see GDScript: Manipulating the SceneTree.
Signal up and call down
editIt means, if you are calling methods on another node, use signals for calling parents (to decrease code complexity) and use methods when calling children (to increase readability and freedom).
What you have learned
edit- How to get a child node.
- How to connect a signal.
- How to set a property of another node (With the ".". You can use it to call their functions too!)
Quiz
edit
- Getting started [ ]
- Making it work
- Making it look good
- Advanced help
- Miscellaneous
<-- previous
back to top
next -->