#FSM + SOs

1 messages · Page 1 of 1 (latest)

green pike
green pike
#

Player controller code

    private void Awake()
    {
        [...]
        _currentState = GetState(HumanStates.Grounded);
        _currentState.EnterState();
    }
    public HumanBaseState GetState(HumanStates stateEnum)
    {
        return StateConfigs?.FirstOrDefault(st => st != null && st.State == stateEnum)?.GetHumanBaseState(this);
    }

Sample SOs

using UnityEngine;

[CreateAssetMenu(menuName = "StateConfigs/Grounded")]
public class GroundedStateConfigs : BaseStateConfigs
{
    public GroundedStateConfigs()
    {
        State = HumanStates.Grounded;
    }
    public override HumanBaseState GetState(HumanStateMachine currentContext)
    {
        return new HumanGroundedState(currentContext, this);
    }
}
#

Sample State

public class HumanRunState : HumanBaseState
{
    private RunStateConfigs configs;
    public HumanRunState(HumanStateMachine currentContext, RunStateConfigs runStateConfigs) : base(currentContext, runStateConfigs)
    {
        configs = runStateConfigs;
    }

    public override void CheckSwitchStates()
    {
        if (!Ctx.IsMovementPressed)
        {
            SwitchState(Ctx.GetState(HumanStates.Idle));
        }
        else if (Ctx.IsMovementPressed && !Ctx.IsRunPressed)
        {
            SwitchState(Ctx.GetState(HumanStates.Walk));
        }
    }

    public override void EnterState(){}

    public override void ExitState(){}

    public override void InitializeSubState(){}

    public override void UpdateState()
    {
        Ctx.AppliedMovementX = Ctx.CurrentMovementX * configs.RunMultipler;
        Ctx.AppliedMovementZ = Ctx.CurrentMovementZ * configs.RunMultipler;
    }
}
cunning nova
#

can you show us this guy too? BaseStateConfigs Also, If you can add a comment line above each function/class with some explanation that would definitely help see whats going on. One thing I DO see that's a bit odd- it looks like these states are SO's (cuz of that CreateAssetMenu) which don't actually use new Constructor() but rather Object.Instantiate - ARE they in-fact SO's?

#

@green pike

green pike
#

Yes they are in fact SOs

#
using UnityEngine;
public abstract class BaseStateConfigs : ScriptableObject
{
    public bool IsRootState = false;
    public HumanStates State;
    public abstract HumanBaseState GetHumanBaseState(HumanStateMachine currentContext);
}
cunning nova
#

ok, that might prove to be a problem. does it actually work when you try to call say... public HumanRunState(HumanStateMachine currentContext, RunStateConfigs runStateConfigs) : base(currentContext, runStateConfigs)

green pike
#

yes

cunning nova
#

well.. thats new! I've never before successfully created an SO using "new" , if that actually works now I gotta go try some stuff (been a huge headache of mine for years!)

green pike
#

I did it accidentally tbh

#

I thought: "Wait. I can delete the MachineStateFactory if I do it with SOs"

cunning nova
#

hmm.. I still get a warning, but it looks like the constructor on an SO does return an object, with the data I pass into the constructor. I was even able to save it with AssetDatabase.CreateAsset! do you even get those warnings? (Sorry I know this is not help with your FSM, but learned something new today- so thanks!!)

#

(imma post this to advanced. see if I can get more info)

green pike
#

no i don-t get it

green pike
#

I have a state machine and I can change, add and delete directly the states from inspector at runtime

#

Idk if its dumb but rn it works

cunning nova
#

nice! let me ask tho. why do you need this array of states? do they contain state-configuartion data or something?

green pike
#

Yes

#

It was my take on how to make it easy to change for one that doesn't know Unity/code

#

And to have idk. Two characters with different states that can be added/removed at runtime

cunning nova
#

do you have a DIFFERENT jump... AH! got it

green pike
#

Yes. Since it's more character, one could be shorter and as such run slower and jump less

#

But maybe he can crouch

cunning nova
green pike
#

I see. But they are usefull for storing and making them self contained

cunning nova
cunning nova
#

(AND the option to create em via a constructor)