0 votes

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

argumentfrombutton = a number.

Then, I have a list of towers organized by id. Such as : tower1race_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.argumentfrombutton.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 ?

in Engine by (184 points)
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:


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()

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 Frequently asked questions and 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 [email protected] with your username.