Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Dumuz |
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?
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 one_test(),func two_test()) and using call() dynamically
Inces | 2021-12-04 10:23
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
Wakatta | 2021-12-04 14:01
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.
timothybrentwood | 2021-12-04 15:05