Left and Right movement not working as expected

These can help you keep track of which state your character is in. Is the character attacking, walking or idle? That sort of thing. You could go further and create a state machine for more complex character states and player interactions, but for a simple game it is sufficient to track the states like in the example.

I’d probably take it on step further and move the states to constants:


local STATE_SWORD_ATTACK = hash("attack_sword")
local STATE_WALKING = hash("walking")
local STATE_IDLE = hash("idle")

function on_input(self, action_id, action)
    if action_id == hash("sword") then
        self.state = STATE_SWORD_ATTACK
        ...

        ...
        self.state = STATE_WALKING -- or STATE_IDLE

The advantage is that there is less risk of typos, especially if you use a code linter/language server.