How return two variable from function

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Mefihl
:warning: Old Version Published before Godot 3 was released.

How return two variable from function?

hi, save values in Array or transform data in String and

Split the string by a divisor string, return an array of the substrings. Example “One,Two,Three” will return “One”,”Two”,”Three” if split by ”,”.

Aquiles | 2016-08-16 20:08

:bust_in_silhouette: Reply From: Mefihl

return [1,2] :wink:

I think this is a better solution

Aquiles | 2016-08-16 20:15

Getting back the values doesn’t work though, as explained here in this feature request:
Python-like list unpacking/destructuring · Issue #7584 · godotengine/godot · GitHub

If I do:

    [x, y] = my_function()

it runs without error but x and y are not changed.

hipi | 2018-07-24 18:06

It’s not as easy as Python, unfortunately. You have to do something like:

var value = my_function()
x = value[0]
y = value[1]

Torajima | 2018-07-25 03:05

Oh, ok, at least it is possible, I thought it wasn’t!
Thank you!

hipi | 2018-07-27 15:38

Hey there! I am a bit late to the party, but in case anyone else comes here and wonders if they can do this without having to memorize the array position of return values, you can also return a dictionary of labeled properties with values like this:

func my_function():
    return {"x":x, "y":y}

Then you can refer to the properties “x” and “y” of your return like this:

var value = my_function()
x = value.x
y = value.y

In this example I used “x” and “y” but you can write whatever you want there (like “abcdef” and then refer to it as value.abcdef).

Good luck gamedevs!

Agamec | 2022-01-09 17:26