Jump to content

Guide to the Godot game engine/Signals and methods

100% developed
From Wikibooks, open books for an open world

Many Godot users will say to you "signal up, call down" if you ask for tips with coding. Just what does this mean?

Signals

[edit | edit source]

Select 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

[edit | edit source]

Just 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

[edit | edit source]

It 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 | edit source]
  1. How to get a child node.
  2. How to connect a signal.
  3. How to set a property of another node (With the ".". You can use it to call their functions too!)


1 What do you use to get a node?

$NodeName
get_node("NodeName")
get_child("NodeName")

2 Are node paths are like file paths?

Yes
No

3 Which way are you supposed to call?

Down
Up


Guide to the Godot game engine

Getting started [edit]
Installation
What is a node?
Programming
Resources and importing
Signals and methods
Your first game
Making it work
Debugging
Input
Physics
Saving and loading
Multiplayer
Making it look good
UI skinning
Animation
Advanced help
Servers (singletons)
Platform specific
Optimisation
Encryption
Exporting
Plugins
Miscellaneous
Helpful links
Authors and contributors
Print version


<-- previous back to top next -->