0 votes

how to pass argument from the signal to the function ?

Godot version Godot 4
related to an answer for: Signal connect invalid argument
in Engine by (21 points)

1 Answer

0 votes

There are 2 main ways of binding aruments to the signal

  1. On connection.

    Gun.connect("shot",self,"on_gunshot",[Gun])
    (This makes gun send reference to itself along every signal it will emit towards script above. These binds are in brackets, they must be in array)

  2. On emission

    Gun.emit_signal("shot",ammo)
    (This makes every emitted signal carry information about current ammo, at the moment of emission. This can be any number of different variables, spearated by comma.)

Finally, connecting method reads these arguments from brackets, first binds on emission, next binds on connection :

func on_gunshot(ammo, gun):
         print(ammo)
         if ammo == 0:
               gun.queue_free()
by (5,252 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.