Mouse entered signal with mouse click if statement not working

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

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 _on_Cookie_mouse_entered(): 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 
:bust_in_silhouette: Reply From: jgodfrey

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")

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.

Warionator | 2020-06-12 00:56