0 votes

extends Area2D

var touch_down := false

func ready():
connect("input
event", self, "oninput_event")

func oninputevent(viewport, event, shapeidx) -> void:

if event.is_action_pressed("ui_touch"):
    touch_down = true
else: touch_down = false

print (touch_down)
in Engine by (76 points)

1 Answer

0 votes

The signal input_event(viewport, event, shape_idx) built into Area2Ds is good for getting single presses, in addition to mouse movement, but in your case where you are checking the button state every frame, I haven't found a good way to use this signal.

Instead, we can use the mouse_entered() and mouse_exited() signals to see if our mouse is in the Area2D before checking if our mouse is being pressed.

Connect your Area2D's mouse_entered() and mouse_exited() signals to your script, then create a new variable to store if the mouse is inside the Area2D. Your code should look something like this afterwards:

var touch_down = false
var mouse_inside_area = false

func _process(delta):
    if Input.is_action_pressed("ui_touch") and mouse_inside_area:
        touch_down = true
    else:
        touch_down = false

    print(touch_down)

func _on_Area2D_mouse_entered():
    mouse_inside_area = true

func _on_Area2D_mouse_exited():
    mouse_inside_area = false
by (122 points)

When i run the game it just prints that touch_down is False
even though I'm clicking/entering inside the Area2D's collision shape

Did you connect the signals? Also, in your Area2D, you need to turn on the setting input_pickable so the Area2D will detect for the mouse.

Ok its working now thx alot

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.
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 webmaster@godotengine.org with your username.