0 votes

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.

enter image description here

What did I miss here?

asked Jan 19 in Engine by Fenisto (63 points)
edited Jan 19 by Fenisto

1 Answer

+1 vote

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

answered Jan 19 by jgodfrey (6,522 points)

My bad. Forgot to fix those 2 "Wasselected". Should be only "wasselected" 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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.