All Questions

Community

A
ThePirateKing
posted to: Coding the pause

Event Bubbling Order

What is the order in which event propagate throughout the scene tree?

How do we know that the `_unhandled_input` method that we have here will be called first and properly `set_input_as_handled()` before the event had visited other nodes with `_unhandled_input` behavior?

Would a click event, for example, propagate based on UI/render layering or would it propagate based on the scene tree hierarchy?

  • Razoric replied

    In Godot, the rendering order is completely separate from the way events and built-in functions are handled. Everything is always based on the scene hierarchy (and the render order is also based on the scene hierarchy.)

    All input events begin at the deepest child node and then move up towards the parent from there. So by the time the parent has received it, we know for a fact that all of its children have already received it.

    Additionally, there are multiple input functions that are called in the following order: _input (called no matter what), _gui_input (only on the relevant Control), _unhandled_input (if an input has not yet been handled and reaches the child), _input_event (only on collision objects.)

  • Nathan Lovato replied

    To complete Razoric's answer, the rules to process input, but also the ready and process callbacks are:

    1.  Nodes get processed in tree order, from top to bottom.
    2. Children get processed before their parent.

    So with the hierarchy

       root
        |-- Camera
        |-- Characters
        |---- Player
        |---- Enemy
    

    The call order will be:

    1. Camera
    2. Player
    3. Enemy
    4. Characters

    Notice how Camera is first because it's the first node in the tree order and doesn't have children, but then we jump to Player and Enemy.

    All the things Razoric explained, like _input() being called first on all nodes, then gui_input, etc. still apply.