- Home /
Pausing a State Machine FlexFSM
Hi again,
I have been researching ways to create an FSM for resource collection, as my knowledge of C# is limited I'm searching for a goldilocks version (not too hard to understand at my current level of knowledge but efficient enough for it not to become a tangled mess.
I have come across FlexFSM which looks simple enough for me to get stuck into and edit however I'm having some problems figuring out how to add a pause when entering or exiting states.
Please don't laugh :)
I tried adding the following to the bottom of the Miner.cs;
public void StartPause() {
StartCoroutine(TestPause());
}
public IEnumerator TestPause(){
yield return new WaitForSeconds(3f);
}
And adjusted in MinerStates.cs;
public override void OnEnter(GameObject owner)
{
miner.LogAction("Entering mine");
miner.StartPause();
}
To try and force a 3 second pause, this obviously did not work :) when loops become nested I have difficulty following actions through them. maybe that will just come with time.
I'm guessing I would need to adjust the following in Miner.cs to allow for pauses but, need some guidance if you have the time.
void Update()
{
if (fsm != null && fsm.IsActive())
{
fsm.UpdateFSM();
currentState = fsm.GetCurrentStateName();
}
}
I have sort of got something working :)
changed $$anonymous$$iner.cs
public bool alive;
public float pauseTime = 0.0f;
void Start()
{
Initialize$$anonymous$$inerData();
$$anonymous$$akeFS$$anonymous$$();
alive = true;
}
void Update()
{
if(!alive) {
StartCoroutine(Pausing ());
}
while(alive){
if (fsm != null && fsm.IsActive())
{
fsm.UpdateFS$$anonymous$$();
currentState = fsm.GetCurrentStateName();
}
}
}
public IEnumerator Pausing() {
yield return new WaitForSeconds(pauseTime);
alive = true;
pauseTime = 0.0f;
}
and added to all enter/exit states on $$anonymous$$inerStates.cs
public override void OnEnter(GameObject owner)
{
$$anonymous$$er.alive = false;
$$anonymous$$er.pauseTime = 3.0f;
$$anonymous$$er.LogAction("Entering $$anonymous$$e");
}
but, it's not quite right, seems to only pause every now and again.
Answer by Baste · Feb 11, 2015 at 08:23 AM
The Waitforseconds does not halt the rest of the script, it just halts that one coroutine.
The easiest way to prevent your miner from moving for 3 seconds after it's created (if that's your goal), is to do this:
bool canStart = false;
void Start() {
StartCoroutine(ActivateAfter(3f));
}
IEnumerator ActivateAfter(float seconds) {
yield return new WaitForSeconds(seconds);
canStart = true;
}
void Update() {
if(!canStart)
return;
//Normal update stuff
}
Sending the float argument in as an argument to the coroutine isn't strictly necessary, but I think it looks nicer like that.
thanks for that I'll give it a shot, what I'm really after is a way to pause the OnEnter, OnExit, Reason, Act functions within a state in $$anonymous$$inerStates.cs which I think would help me debug problems later on