All Questions

Community

Grominet
posted to: Air Dash (solution)

Why use a Timer for the dash?

Hello,

Why use a Timer for this? This seems to work the same:

func physics_process(delta: float) -> void:
velocity = owner.move_and_slide(direction * dash_impulse, owner.FLOOR_NORMAL)
_state_machine.transition_to("Move/Air", {velocity = velocity})

Moreover, is it really that obvious to use a State for the dash? It looks more proper, but as it is only an instant boost in velocity, I am not so sure.

  • Nathan Lovato replied

    The timer controls the duration of the dash. Try with different time values, you'll see it's not the same as your code: the character moves only in the dash direction during the dash, and they keep going that way.

    Then, as to why you'd use a state: it's not a question of making things right but rather making a state for everything that's likely to change. With a dash state object, it's really easy to:

    1. Remove the dash from the game

    2. Toggle off the ability to dash (your character gets stunned, enters a special zone), all it takes is to temporarily remove the node from the tree

    3. Control which state you can transition to from a dash: fall, ledge grab, but also perhaps slam the ground if you dash downward, things like these.

    4. Change the dash mechanic itself, which is really likely to get modified at some point. For example, how should it interact with enemies? Can you use it to inflict damage or break things? Any of that means more dash code.

    If you use a finite state machine, you often want to leverage it for every unique behavior the character has.

  • Grominet replied

    Oh right sorry, I overlooked the way your code works :(
    And thanks for the very detailed explanation of why using a state is relevant here. Very clear now :)