how to deal with the variable number of arguments in the call() function?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By mymokol

I need to call the function call(function_name, arg1, arg2…), but all the arguments are in one array of varible length. (the function in which i’m calling call() of a different node might end up getting arguments like this: function_name = fn1, args = [argument], or like this: function_name = fn2, args = [position, velocity, UP].

The call() function appears to only allow passing the args one by one, not in an array.

How do I deal with this?

:bust_in_silhouette: Reply From: Gluon

Well I know of two possible ways to deal with this, you can pass a dictionary to a function as an argument like this

func Example(Dict={}):

and you can then pass the function example a dictionary which you can unpack within the function. Alternatively you can use an autoload script as a way of passing information around. If you have an array variable in an autoload script you can add items and read items too it from any other script in an active scene.

thanks, i’ve thought about this, the dictionary method limits the functions i can call just to those built specifically for being called by this, but that’s fine i guess. The autoload is too difficult for me to understand, but thanks for helping!

mymokol | 2022-06-20 06:58

:bust_in_silhouette: Reply From: dynid

You can do this with the callv() function:

callv(fn2, args)
or
callv(fn2, [position, velocity, UP]
will both work.