How to properly use the .has() function? (check array without quotes)

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.

It should be _stance.name without the quotes.

Shouldn’t

if focus_stances.has("_stance.name"):

be:

if focus_stances.has(_stance.name):

?

No, I had it like that before but added the quotes thinking it would solve it.

[“block”, “attack”],stealth
false
[“block”, “attack”],crouch
false
[“block”, “attack”],prone
false
[“block”, “attack”],block
false
[“block”, “attack”],stealth
false
[“block”, “attack”],block
false

That is the result with:

func _on_changed_stance(_stance : Stance):
	if focus_stances.has(_stance.name):
		focus_mode = true
	else:
		focus_mode = false

Any ideas?

No, I had it like that before but added the quotes thinking it would solve it, but the result was the same. Any ideas?

Can you show how you are printing this? how is focus_stances defined? and Stance?

I’m sorry everyone, I’m an idiot. I was using a setter to emit the signal each time the boolean changed. (I still don’t really understand how to use set get).

But this was the problem:

var focus_mode: bool :
	set(new_value):
		changed_focus.emit(focus_mode)

For some reason it kept the value as false.