#Stop clicks with Area2d?

1 messages · Page 1 of 1 (latest)

rich willow
#

Trying to make some simple point and click mechanics, and I need some interactable (clickable) objects. I made them out of a simple Sprite2D with an Area2D and CollisionShape2D.

The character movement uses unhandled_input to check the clicked spot and physics_process to actually update it if it can (i.e. not clicking a UI element).

What I can't quite figure out is if there's a way to essentailly stop clicks with an area2d input signal in the same way a UI element can. I know I could try and convert all interactable objects into Control nodes, but that makes it harder to do certain things with the objects too, so I'm not sure I want to do that. Also it would be tedious.

My first thought was to use set_input_as_handled on click event but it seems that won't work because the priority isn't actually higher than unhandled_input (or something like that? All I know is it doesn't work. Is it just a timing thing?). I also tried putting get_viewport().physics_object_picking_first_only = true and get_viewport().physics_object_picking_sort = true in the ready function of the interactables and raised the Z index but still no luck. So I'm not quite sure what I need to do.

sinful aurora
#

Area "input_event" runs after unhandled input, because it counts as Physics Picking Event, if i am correct, so that won't work. I would suggest either trying to detect if an area was clicked in your character and discard it there, or you can try to use _input() func in your area, but you will have to check if it is inside the area manually, cause this one checks the entire viewport for input events.

#

Not sure if it's best solution, but that should work: ```func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
if event.is_pressed() and is_valid_move():
print("Clicked ground or smth")

func is_valid_move() -> bool:
var para = PhysicsPointQueryParameters2D.new()
para.position = get_global_mouse_position()
para.collide_with_areas = true

var result = get_world_2d().direct_space_state.intersect_point(para)
print(result)

return result.size() == 0 ```
rich willow
#

Oh, that does work. Thank you so much!

static marten
rich willow
# static marten func _ready() -> void: get_viewport().physics_object_picking_sort = true ...

I originally had the code in _input_event, but I moved it to _unhandled_input so that I could block clicks on UI too. I have also now changed things around so that it uses the navigation mesh and navigation agent to make some things a bit easier.

But thank you for the input. I don't know a whole lot about the innerworkings of Godot, so why those work with _input_event and not _unhandled_input was a little lost on me. It's a good thing to know.