Hot to unpack param Array in separated args?

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

Is there a way of unpack an array in a argument of a function? Ex:

   func myPrint([num1, num2]):
    	print("num1: " + String(num1))
    	print("num2: " + String(num2))
    
    func main():
        var params = [1, 2]
    	myPrint(params)
:bust_in_silhouette: Reply From: jgodfrey

Not sure if this is exactly what you’re after, but I think the closest thing in gdscript is callv(). Here’s an example:

func _ready():
	var some_array = [1,4,2,3]	
	callv("printall", some_array)

func printall(a,b,c,d):
	print(a+d)
	print(b+c)
	print(c-b)
	print(d-a)

Results in:

4
6
-2
2

Thanks for the answer, but unfortunately it doesn’t apply to my real case. For the example, it is perfect, but i used it for simplicity. In my real case I’m already using a function call:

func someFunc(newScreenPath, newScreenArgs):
    thread.start(self, "loadNewScreen", [newScreenPath, newScreenArgs])

func loadNewScreen(scenePath: String, sceneArgs: Array):
    # logic here
    pass

I don’t think that I can use one call inside another.

Felipe | 2023-03-12 03:34

Hmmm… I guess I don’t really understand what you’re trying to accomplish. In the case of a thread.start() call, that 3rd arg is just a Variant (at least, for v3.2). So, you can pass whatever is appropriate there (Array, Dictionary, …). And, once you have it in the called function, you can unpack it using any appropriate mechanism for the data type.

I’m sure I’m probably just missing the point…

jgodfrey | 2023-03-12 04:04