State Machine Design: Multiple Stun States vs Stun Reason Attribute

Hello, I was wondering whether it’s better to create separate states in a state machine - such as GuardBroken, PerfectBlockStunned, AttackStunned, etc. - or instead use a single state with a sub-attribute like “StunReason” (or something similar).

For example, if a player is stunned due to a perfect block, an attack stun should not be able to override that stun because the perfect block stun has a longer duration. Another idea I had was to store the stun end time, and when applying a new stun, check whether its duration is shorter than the current one - if so, simply return false and do not override it.

Below is my current state machine implementation:

function CombatUtils:SetState(player: Player, newState: string)
    local currentState = player:GetAttribute("State") or "Idle"

    if not CombatConfig.AllowedTransitions[currentState] then return false end
    if not CombatConfig.AllowedTransitions[currentState][newState] then
        warn(`Invalid transition: {currentState} → {newState}`)
        return false
    end

    local version = (player:GetAttribute("StateVersion") or 0) + 1
    player:SetAttribute("StateVersion", version)
    player:SetAttribute("State", newState)
    
    print(`Transiting from: {currentState} → {newState}`)

    return true, version
end

function CombatUtils:CanTransition(player, newState)
    local character = player.Character
    
    local currentState = character:GetAttribute("State")
    local transitions = CombatConfig.AllowedTransitions[currentState]
    
    if not transitions then
        return false
    end
    
    return transitions[newState] == true
end

-- Table for allowed transitions
AllowedTransitions = {
    Idle = {
        Attack = true,
        AerialAttack = true,
        Block = true
    },
    
    Attack = {
        Idle = true
    },
    
    Block = {
        Idle = true
    },
    
    CritAttack = {},
    AerialAttack = {},
    RunningAttack = {},
    Stunned = {}
}

I’m in favor of storing the stun end time and checking which is greater when applying a new state.

This way, the stun duration already elapsed can be considered. Say for example that a perfect block stun is 5s and an attack stun is 3s. If 4 seconds have elapsed during a a perfect block stun, you would want the attack stun to override it in that case, since the duration is longer than the duration remaining on the first stun.