Is there a If Touching Mouse Pointer-like statement?

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

Im generally new to GDScript, so I dont really know all the code. Even in the 2D forums, I couldnt find anything that works like, if the cursor is touching the sprite then the if statement starts rolling, simple. I tried using the _on_mouse_entered and _on_mouse_exit, but it didnt work. I dont know if those statements are used for something else, or Im just not doing it right. Im trying to make it so a animation plays when the mouse starts hovring over the sprite, then a different animation while its hovering. The same for when not. When it leaves the cursor, animation, the whenever its not touching the cursor, a different one. I tried this at some point

func _on_mouse_entered:
    isHover = true
    #do stuff
    while isHover == true:
        #do stuff

func _on_mouse_exit:
    isHover = false
    #do other stuff
    while isHover == false:
        #do other stuff

If you put a while loop inside the _on_mouse_entered function, it will lock up the game since it won’t register anything until after the loop. Since the entire game is like a while loop, you should put a check in a process function for isHover instead, if what you want needs to be repeated each frame. It really depends on what specifically you want to do when the mouse is hovering over the Node.

exuin | 2021-06-07 12:46

Since the entire game is like a while loop, you should put a check in a procces function for isHover instead

ummm… what do you mean by check. I looked it up, and checked the forums for anything related to that, but couldnt really find anything. Is that a built in function, like,

check(isHover == true):

like, how does that work. I dont really understand GDScript. Im used to Java, Javascript, and some #C

YumeSharkMekzGemz | 2021-06-07 18:08

It’s not a function, what I meant was to put an if statement inside a process function.

exuin | 2021-06-07 18:43

:bust_in_silhouette: Reply From: gioele

You could use Area2D as a base node.
Add a script and connect its signals _on_mouse_entered and _on_mouse_exited

In those methods alter the state or even set_process(true) to enable some kind of behavior.

An example:

var hover = false
func _process(delta: float) -> void:
	if hover:

func _on_Area2D_mouse_entered() -> void:
	hover = true

func _on_Area2D_mouse_exited() -> void:
	hover = false

If you want to manage complex animations, I think you should check Animation Tree, in that case you could define 2 base looping animations for hover/non-hover and also a couple of transition animations. In that case you will call the animation tree on the mouse enter/exit functions setting transition to main animations (letting it manage the transitions).

But first check if something like the solution above could solve your problem.

:bust_in_silhouette: Reply From: Asthmar

Like exuin said, you are missing a process function

func process function:
if isHover == true:
# do stuff

Process functions are called for every frame. So basically they are constantly checking and running the code inside it. You can’t put a while function under your mouse function because it only runs as soon as the mouse enters or exits.