make a variable name by mixing 2 variables ? is there a way ?

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

Hello there !
I have a button sending an argument INT.

argument_from_button = a number.

Then, I have a list of towers organized by id. Such as : tower_1_race_2, tower_2_race_3 etc…

So. Here is where I’m trying to mix these up :

I’d like to create something like : tower_.argument_from_button._race_2 (this is obviously not good, just to give you an idea).
The end result, read by godot would be : tower_X_race_2

I used to do this in php a while ago. But I’m fairly new to DGscript.
Any suggestions ?

func get_tower(tower_name : String, race: int):
    if not tower.get(tower_name):
        return # no tower found with input string, typing error
    elif race == 1:
        return tower.get(tower_name).race1
    elif race == 2:
        return tower.get(tower_name).race2
# etc

Using get() isn’t great as it doesn’t give you type safety in editor. It might be better to use a dictionary:

tower.gd

var tower_data = {
"tower1" : {"race1" : "i'm tower data", "race2" : "daaata", ....},
"tower2" : {"race1" : "i'm more tower data", "race2": "datatatata", ...}
}

func get_tower(tower_key : String, race_key):
    return tower_data.get(tower_key).get(race_key)

Note the above is Dictionary.get() not Node.get()

timothybrentwood | 2021-08-23 15:56