Hi all, I'm making a small little roguelite where the player upgrades stats with cards. Thing is, I don't know how to make this code... good? I'm using an export variable to determine what variable of the players the card will change, but I can't just go "player.stat += change" The only way I can is with this kind of code, shortened to make is bearable.
Thanks in advance.

match stat:
	"speed":
		player.speed += change
	"reload":
		player.reloadSpeed -= change
	"shotSpeed":
		player.attackSpeed -= change
	"exp":
		player.expMult += change
...
  • xyz replied to this.
  • Sea_Ferret You mean accessing properties by their name contained in a string? For that you can use get() and set() methods:

    var prop_name = "speed"
    player.set(prop_name, player.get(prop_name) + 1)

    But maybe a better approach would be to store those properties in a dictionary. It results in much cleaner code:

    var props = {
    	"speed": 1, 
    	"exp": 1
    }
    props["speed"] += 1

    Sea_Ferret You need to use get_node() function to get a reference to player's node. Then you can access properties in that node's script via dot syntax:

    var player = get_node(path_to_player)
    player.speed +=1

      xyz thats not the issue. i can get the player perfectly fine. i cant easily change the players variables. like the stat variable is a string. then change is a float. how can i simplify this so its just "player.stat += change"

      • xyz replied to this.

        The solution may be to store the values in a Dictionary or Array.

        Example:

        var d: Dictionary = {
            "speed": 0.0,
            "reload": 0.0,
            "attack": 0.0
        }

        d["speed"] += change

        Sea_Ferret You mean accessing properties by their name contained in a string? For that you can use get() and set() methods:

        var prop_name = "speed"
        player.set(prop_name, player.get(prop_name) + 1)

        But maybe a better approach would be to store those properties in a dictionary. It results in much cleaner code:

        var props = {
        	"speed": 1, 
        	"exp": 1
        }
        props["speed"] += 1