Godot Version
4.2.1
Question
In my game I use states for the character.
I’m trying to have a boolean that would change to true if the current state is either block or attack. So I emit a signal each time the state is changed and then check an array that I created that contains the words “block” and “attack” (so that I can easily add other states to check afterwards if I need to).
But I’m guessing that it doesn’t work because the .has() is checking the array including the quotation marks of each value?
This is the code I’m using:
func _on_changed_stance(_stance : Stance):
if focus_stances.has("_stance.name"):
focus_mode = true
else:
focus_mode = false
I printed the array, the current state and the value of the boolean and got this:
[“attack”, “block”],crouch
false
[“attack”, “block”],stealth
false
[“attack”, “block”],block
false
[“attack”, “block”],upright
false
[“attack”, “block”],block
false
[“attack”, “block”],upright
false
[“attack”, “block”],block
false
It’s turning false even if it matches. So that’s why I’m guessing that it’s because of the quotation marks, how do I make it check without the quotation marks?
Edit: I solved it, the setter that I was using was the problem (I don’t really know to use get/set properties yet).
var focus_mode: bool :
set(new_value):
changed_focus.emit(focus_mode)
I solved it by removing the setter and using this instead:
func _on_changed_stance(_stance : Stance):
var current_focus_mode : bool
current_focus_mode = focus_mode
if focus_stances.has(_stance.name):
focus_mode = true
else:
focus_mode = false
if not current_focus_mode == focus_mode:
changed_focus.emit(focus_mode)
Now it works even better because it emits the signal only when focus_mode is actually changed (as I intended). Not each time I change state.