sprite.visible = true\false doesn't work in some cases when using mouse_entered() signal

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Fenisto

I have this code that makes a one of 2 (for this example, actually more) node’s sprite visible if the cursor enters it’s collision polygon area and makes it invisible when it exits. “Selected” is a sprite. All invisible by default.

func _on_Area1_mouse_entered():
	$Heroes/Hero1/Area1/Selected.visible = true 
func _on_Area1_mouse_exited():
	$Heroes/Hero1/Area1/Selected.visible = false

func _on_Area2_mouse_entered():
	$Heroes/Hero2/Area2/Selected.visible = true
func _on_Area2_mouse_exited():
	$Heroes/Hero2/Area2/Selected.visible = false

But now I want to make it so that a sprite that became visible would stay that way until another one gets visible. Only then the fist one would become invisible.
I do it like that:

var was_selected = null

func _on_Area1_mouse_entered():
	$Heroes/Hero1/Area1/Selected.visible = true
	if was_selected != null: 
		was_selected.visible = false
func _on_Area1_mouse_exited():
	was_selected = $Heroes/Hero1/Area1/Selected
	
func _on_Area2_mouse_entered():
	$Heroes/Hero2/Area2/Selected.visible = true
	if was_selected != null:
		was_selected.visible = false
func _on_Area2_mouse_exited():
	was_selected = $Heroes/Hero2/Area2/Selected

When the cursor exits a node’s area it’s sprite’s path gets stored in a variable and when the cursor enters another area the previously stored sprite is made invisible.

Now if I move my mouse slowly everything works as intended. But if I move it fast then several nodes stay visible at the same time(I have more than 2 nodes) and others do not get visible or invisible if you hover over them. But if you enter-exit a few times then they switch how it was intended.

What did I miss here?

:bust_in_silhouette: Reply From: jgodfrey

A few comments…

First, you have two different variations of your tracking variable (Was_selected and was_selected) in the posted code. I’m not sure how that would work at all, so I assume that’s just an issue in the posted code and not the code you show running in the video.

I’m only guessing, but I assume the problem is due to the timing of the various entered and exited events.

If you really only want the last touched item to be made visible, it might be easier/safer to handle everything in the entered event.

So, in the entered event, set everything to visible = false first (likely by placing all items of interest in a common group and spinning through them all), then set the current item to visible = true

My bad. Forgot to fix those 2 “Was_selected”. Should be only “was_selected” as you assumed.

Tinkering with groups I managed to make everything in the game invisible. Even if only sprites are in a group…

But following on your idea I just made a function that does that without groups. Works fine. Thank you.

Will study how to make groups. The documentation is often lacking is explanations unfortunately.

Fenisto | 2020-01-19 19:22