+3 votes

I understand how to call a function for every member of a group, like in the Docs:

get_tree().call_group(0, "guards", "player_was_discovered")

But is it posible that I can add a variable to the function, like by a normal function, for example:

func player_was_discoverd(variable):
in Engine by (330 points)

1 Answer

+9 votes
Best answer

Yes, just append the variables you want to pass in the end:

get_tree().call_group("guards", "player_was_discovered", 3, "Hohoho, got ya again!")

You can then use them like any normal arugment:

func player_was_discovered(times_catched, taunt):
    print("You have been discovered by guard number %d. He yells: %s" % [times_catched, taunt])
by (10,315 points)
selected by

I tried it and it works with one or two variables, but if I have three or more variables it doesnt work any more. Is it necessary to change something, if I use three variables or can I only use one or two variables?

If you're passing more arguments to the function like here:

get_tree().call_group("guards", "player_was_discovered", 1, 2, 3, 4)

you also have to adapt the function of course:

func player_was_discovered(arg1, arg2, arg3, arg4):
    print("%d %d %d %d" % [arg1, arg2, arg3, arg4])

You also can pass an array of arguments:

get_tree().call_group("guards", "player_was_discovered", [1, 2, 3, 4])

Then you can have a generic callback:

func player_was_discovered(args):
    print("%d %d %d %d" % args)

Thank you very much!

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.