This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+1 vote

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 onmouseentered and _onmouse_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
Godot version 3.3
in Engine by (18 points)
edited by

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.

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):
    #output

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

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

2 Answers

+1 vote
Best answer

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:
        rotate(0.01)

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.

by (291 points)
selected by
+1 vote

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.

by (831 points)
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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.