Match dict key and object variable

Godot Version

4.11

Question

Trying to create universal functions for my abilities in pokemon game. So pokemon has stats (pokemon.stats.attack, pokemon.stats.defense, etc). Ability.effects is an array, effect[0] changes stats of the pokemon. I decided to make effect[1] a dictionary, with key as a stat and value as a modifier. So we have ability that doubles attack, effect[1] = {“attack”: 2}.

		if current_poke.stats.ability_active == false:
			for key in current_poke.base_stats.Ability.effect[1].keys():
				current_poke.stats.{key} *= current_poke.base_stats.Ability.effect[1][key]
			current_poke.stats.ability_active = true

key is not a reference of a variable. %s does not work. in Python I would use f’current_poke.stats.{key}'. I don’t see an easy way of doing it, need help (easiest way would be write if key == “attack”: current_poke.stats.attack *= Ability.effect[1][key].

Can’t you do “current_poke.stats[key]”?

1 Like

So basically attack is an attribute of whatever class type stats is.
One way is to use the base Object method “set, to set the stats “attack” property to the new value.

if current_poke.stats.ability_active == false:
	for key in current_poke.base_stats.Ability.effect[1].keys():
		var curr_atk_val = current_poke.stats.get(key)
		var effect_mult =  current_poke.base_stats.Ability.effect[1][key]
		current_poke.stats.set(key, curr_atk_val * effect_mult)
	current_poke.stats.ability_active = true

You might want the stats to handle that themselves by just having a function that takes the effect dictionary.


Yet an other solution is to make stats a dict so that you can index your different stats (for consistency and best perfs, stat type can be either defined as an enum value, or a StringName)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.