Editing a variable through a string
Asked Answered
M

3

0

Hi everyone !

I'm trying to change a global variable (of an upgrade) through another node.
I have an upgrade menu in which there are many of them and each have and export variable written as upgrade = "speed_upgrade", or upgrade = "bomb_upgrade". I'd love to have my Global script get that name, find the variable with the same name and update it.
What i'd love is to find a way to transfer the upgrade var from my UI (to know which is selected) to my Global (easy, just have another upgrade var in Global) and then find that variable by name in my Global and update, like this :

Global.upgrade = upgrade
Global.get(upgrade) += 1

Unfortunately, only the first line works (because obviously the second line finds what's stored in the variable and tries to update it... nowhere ?

I hope it's not to confusing. Thanks so much for the time.

Have a good one !

pink

Meggs answered 27/10, 2023 at 18:22 Comment(0)
G
0

The Object.set() method might do what you want. It lets you specify the property name as a string.
https://docs.godotengine.org/en/4.1/classes/class_object.html#id6

Grouping answered 27/10, 2023 at 19:49 Comment(0)
J
0

An easy way to do this is to put a layer of abstraction between your UI and the globals and then reference those when needed. So something as simple as:

# a simple variable will act as reference to the global
var _uiBombUpgrade = Global.bombUpgrade


# then make a function and run it when you want the update to occur
func _globalUpdater():
    if Global.bombUpgrade != _uiBombUpgrade
        Global.bombUpgrade = _uiBombUpgrade
Jaimiejain answered 27/10, 2023 at 20:14 Comment(0)
M
0

Thanks to both of you i managed to do it (which means i probably have a few too many lines of code).
You saved my evening thank you ! And many more evenings to come !

Have a great one people. Thanks !

Meggs answered 27/10, 2023 at 22:16 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.