#FSM + SOs
1 messages · Page 1 of 1 (latest)
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;
}
}
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
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);
}
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)
yes
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!)
I did it accidentally tbh
I thought: "Wait. I can delete the MachineStateFactory if I do it with SOs"
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)
no i don-t get it
If you are intrested, this is what I achieved
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
nice! let me ask tho. why do you need this array of states? do they contain state-configuartion data or something?
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
do you have a DIFFERENT jump... AH! got it
Yes. Since it's more character, one could be shorter and as such run slower and jump less
But maybe he can crouch
btw- this was the best answer I got on using new for SO's : #archived-code-advanced message
I see. But they are usefull for storing and making them self contained
cool, I like that
indeed, plenty of times I wanted an SO just so I could have an asset to reference from a component or other asset.
(AND the option to create em via a constructor)