+1 vote

I don't understand how something so simple I can't do. I'm making a cookie clicker rip off as a practice project, and I want the cookie to be able to detect when the mouse is over it, then detect if the user press mouse button, if so, add a score to the score variable. So I made a signal to the instanced cookie scene func onCookiemouseentered(): and in that, I have the if mouse click statement, but it doesn't work. If I don't have the mouse click if statement, it will add 1 to the score when the mouse enters, which is fine. I don't understand why it's not reading the mouse click and doing the code inside. In the input map, I have "click" as an action and its set to Device 0, Left button, which I assume is left click. My code is below:

extends RigidBody2D
var score = 0

func _on_Cookie_mouse_entered():
    if Input.is_action_pressed("click"):
       score += 1 
in Engine by (16 points)

1 Answer

+1 vote
Best answer

The problem is that the mouse_entered event only fires in the single frame where the mouse cursor entered the collision shape. So, by the time you enter the shape, and then press the button, you're no longer in the event.

I'd bet that if you first press and hold the mouse button and then move the pointer into the shape, your score will register. That's because you would have entered the shape with the button already in a pressed state, which will satisfy your logic.

Really though, you probably want to just wire the input_event instead of the mouse_entered event. There, you can just check your action. So, something like this:

func _on_RigidBody2D_input_event(viewport, event, shape_idx):
    if event.is_action_pressed("click"):
        print("Click Event")
by (16,676 points)
selected by

Yeah, instead I made a boolean variable mouseIn set to true when entered and false when out. I thought thatmouse_entered was something that kept running, not a single flair.

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.