|
|
|
|
Reply From: |
neikeq |
Yes, Godot’s event system is based on signals.
The Godot documentation has this brief introduction to them from GDScript: GDScript reference — Godot Engine (latest) documentation in English
Here is some extra information:
The connect
method has another optional parameter to specify the connection flags (you can use multiple flags with the | operator):
emitter.connect("signal", node, "method", [], CONNECTION_DEFERRED | CONNECTION_ONESHOT)
- Deferred connections (
CONNECTION_DEFERRED
) queue their signal emissions and the connected methods are not called until idle time.
- Oneshot connections (
CONNECTION_ONESHOT
) automatically disconnect themselves after the first emition.
You can also connect signals in the editor by selecting a node and clicking the plug button in the Scene dock:
This will popup a dialog with the list of signals. Signals declared in scripts are also visible. You can select one and click Connect…, which will popup this dialog:
In this dialog you can select a target node and method (optionally marking Make Function to create the method). You can also bind extra parameters if the target method requires them, and mark connection flags.
This basic example increases the button text by 1 when you press it:
extends Node
var counter = 0
onready var button = get_node("MyButton")
func _ready():
button.set_text( str( counter ) )
button.connect("pressed", self, "_my_button_pressed")
func _my_button_pressed():
counter += 1
button.set_text( str( counter ) )
Hi neikeq.
So if I’m not mistaken, connections is signaling an action to target node when condition is met or it just only set the flag?
alextro | 2016-02-23 04:04
Signals are emitted when certain action happens. For example, the BaseButton
class emits the signal pressed
when the button is clicked. This will call all the methods that are connected to that signal.
I updated the answer with an example increasing a counter everytime a button is pressed.
neikeq | 2016-02-23 11:30
Ah I’m starting to understand.
Is it necessary to pass a variable as a string when set a text?
alextro | 2016-02-23 13:09
Yes, that’s why I use str(). Otherwise you would get an error like this:
Cannot convert argument 1 from int to String.
neikeq | 2016-02-23 14:28