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 :slight_smile:

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 :slight_smile: 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();
        }
    }

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.