0 votes

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 
       print(score)
asked Jun 11 in Engine by Warionator (15 points)

1 Answer

0 votes
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")
answered Jun 11 by jgodfrey (5,742 points)
selected Jun 12 by Warionator

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 How to use this Q&A? before posting your first questions.