How to access more than one variable from another script?

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

I need to access a set of variables from script ‘A’ which have a certain value. The value keeps changing on the process. Now I need to access all of them into another script with their value. If I do get_node(), or signals or group it’s time consuming because I have lot of variables. Example: I have a list of food items (more than 20 variables). Each have their own value and this value changes on the process. Now I need to access the food list and their values in another script ‘B’. Now I don’t want to do get_node() for every variable. Is there any easier way to do this. If I add the food items in a dictionary and add a value, will i able to change the values like before. Is there any better way to do it. Please help me out! Thank you in advance

If I do get_node(), or signals or group[s]

Those are three completely different things with different use cases.

I have a list of food items (more than 20 variables)

You mean you have a script A with 20 variables describing food items?

Now I don’t want to do getnode() for every variable

You don’t have to call get_node() each time, simply store its return value:

var f = get_node("FootItems")
print(f.ingredient1)
print(f.ingredient2)
# ...
print(f.ingredient20)

If I add the food items in a dictionary and add a value, will i able to change the values like before.

What let’s you think you wouldn’t be able to? A dictionary is not immutable. However, I fail to see how that would be any easier than accessing the variables individually?

var f = get_node("FootItems").ingredients
print(f["Key1"])
print(f["Key2"])
# ...
print(f["Key20"])

This probably would be easier to solve if you provided your code.

njamster | 2020-08-08 20:35

Thank you for the comment!
My current code is too long so I will try to make shorter.
Let’s keep it with only 2 variables for now

Script A

Var apple = 0
Var orange = 0

Func _on_produce_button_pressed():
apple += 10
orange += 10
Print(apple)
Print(orange)

[now I press produce button few times and say I have changed the
values like apple = 50, oranges = 100]

Script B

Func _on_sell_button_pressed():
Var f = get_node(" ")
f.apple -= 10
f.orange -= 10

[now again I press the sell button few times and
I have apple = 30, orange = 90]

Script C

Func _on_sent_button_pressed():
Var f = get_node(" ")
f.apple -= 10
f.oranges -= 10

[Now I press the button again and I have, apple = 20, orange=40]

Script D

Func_ready();
Var f = get_node(" ")
Print(f.apple)
Print(f.oranges)

Now I get the same result what I expected. But when there
Is more than 20 or 30 food items, its bit a confusing and
frustrating to do the same thing over and over again in another
Script.

If I use dictionary like this
Var food_items = {“apple” : 0, “orange” : 0}
How can i code to change its values and handle it in another
Script like above . As dictionary looks easy. I am new to coding
So please help me out. Thank you in advance.

Mani Vannan | 2020-08-09 13:46

:bust_in_silhouette: Reply From: njamster

If I use dictionary like this var food_items = {"apple" : 0, "orange" : 0}

Then e.g. Script B would look like this:

var f = get_node("<NodePath>")
f.food_items["apple"] -= 10
f.food_items["orange"] -= 10

There is no shorter way of changing the value of X variables than using X lines of code. If all variables share the same type (e.g. are Integers) and are decreased by the same amount (e.g. 10) you could iterate over all keys in the dictionary:

for key in get_node("<NodePath>").food_items.keys():
    food_items[key] -= 10

However, in that case you don’t even need a dictionary, a normal array will suffice!