+1 vote

I want to dynamically call a variable in a custom function. I tried:

func custom_function():
var one_test: 0
var two_test: 0
var three_test: 0

print(some_global_variable + "_test")       #would print : one_test
var current = get(some_global_variable + "_test")
print(current)      #Prints: Null

get() just returns null. Is there a way to call the variables within the method dynamically?

Godot version 3.4
in Engine by (355 points)

Thats right, get() does not work for in-method variables. I had similar problem some time ago, I am pretty sure You can also replace this code structure with signals( calling one method, but have different binded arguments ) or by having multiple methods(func onetest(),func twotest()) and using call() dynamically

So get is a function of Object with custom_function being also

It seems obviously ridiculous to want function a to get variabes from funtion b without exposing them somehow

get() is used when you don't know whether an object has a member variable or not e.g.:

if object.get("cool_variable"):
    do_cool_thing(object.cool_variable)
else:
    do_other_thing(object)

In your case you always know what function variables you have because you define them in the function so it doesn't make sense forget() to find them.

Please log in or register to answer this question.

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.