How to set input as handled on a node that also has an unhandled input function

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

I have a player node that is an area2d node. I want to be able to move the player around when clicking on the map, and also to be able to interact with other objects. So when I click outside the area2d, the player will walk to the click point. When I click inside the area2d, the player will interact with something. But the player also moves towards the click point. I put get_tree().set_input_as_handled() but it didn’t work. What do I do?

Edit: Checked the docs again. Apparently _unhandled_input() is called before the _input_event() function. Not sure if that’s really ideal, but I’m not a game engine designer.

Might need more info. Are you for sure overriding _unhandled_input instead of _input? Are you using “pickable” for the Area2Ds? (those kinda go through stuff other than unhandled_input and are so it wouldn’t help anyway.)

MonsterVial | 2020-09-18 12:42

I really wish there were some better way to input code.

func _input_event(_viewport, event, _shape_idx):
    if event.is_action_pressed("click") and can_interact:
        get_tree().set_input_as_handled()
        for body in get_overlapping_areas():
            if body.has_method("show_dialogue"):
                body.show_dialogue()
                set_active(false)
                return
            elif body.has_method("change_scene"):
                body.change_scene()
                return
            elif body.has_method("add_item"):
                body.add_item()
                return
		
func _unhandled_input(event):
    if event.is_action_pressed("click"):
        destination = get_global_mouse_position()
        move_left = destination.x - position.x > 0
        if move_left:
           ray.cast_to = Vector2(50, 0)
           movement = SPEED
        else:
           ray.cast_to = Vector2(-50, 0)
           movement = -SPEED
        moving = true

I’m pretty sure I’m going through _unhandled_input instead of _input. The function is called _unhandled_input. I know that both functions are getting called because they both perform their functions correctly (except for the handled input thing).

exuin | 2020-09-18 17:25

:bust_in_silhouette: Reply From: exuin

I solved it by just cancelling movement every time the character interacts with something.

Best answer so far thanks self

exuin | 2020-09-18 17:34