Author Topic: Optimization when dealing with high numbers of FSMs  (Read 148 times)


  • Playmaker Newbie
  • *
  • Posts: 34
    • View Profile
Optimization when dealing with high numbers of FSMs
« on: January 23, 2021, 01:56:26 PM »
Hi there, I have some questions related to FSM performance and optimization hopefully someone can give me some guidance on!
My project involves a prefab NPC (just a single 2D sprite) which has a FSM containing around 100 states (all pretty simple actions that control a schedule the characters moves through- eg navigate to bar, wait, navigate to roof, wait, etc). As the game progresses the amount of these NPCs increases into the hundreds, sometimes over 1000 simultaneously. Everything is working great (playmaker has been incredible), but it does get to a point where it starts crashing on iPhone- I'm guessing its just running out of memory.
I have been through all the actions and optimized heavily based on tips in other threads:
-No 'find object' or other expensive actions
-No 'every frame' style actions.  etc

So... a question specifically about the FSM component itself, as I realize there might be some opportunity for broader optimization. The NPCs go into a waiting state for 5 minutes at each location they visit. This is just a single state with only a 'wait' action inside it. But of course the entire FSM component is still 'active' (containing the 100 states and 20 or so variables).. is the rest of this FSM taking up a 'cost'? Or is the current state it is in the only relevant thing here?

Optimization ideas:

1. I thought maybe instead, when the NPC enters their wait state, they could send an event to a new FSM which only has 2 states in it, a wait state and a 'return to active' state. This wait state could deactivate the original larger FSM component for those 5 minutes, then when complete, it would reactivate the main FSM component and return it to previous state. Thus the majority of NPCs on screen would have inactivate FSMs at any given moment -Would having the FSM component deactivate actually help though?? Or is activating a small FSM when deactivating the original large FSM having a 0 net change.

2. Does deactivating array/hash list proxy components when not being used help at all? Each NPC has a few of these that they use only at certain parts of their schedule.

3. Instead of deactivating the FSM component, would deactivating the whole object be a bigger saver? I could replace my NPCs with a static sprite when they are in their 5min wait state.

Any small optimizations multiply out massively as the NPCs increase. So any guidance here would be amazing!


  • Administrator
  • Hero Member
  • *****
  • Posts: 5932
    • View Profile
    • jinxtergames
Re: Optimization when dealing with high numbers of FSMs
« Reply #1 on: January 23, 2021, 03:04:02 PM »
if you are in a wait, the other parts of the fsm are not doing anything.
splitting and disabling would not do anything not even space up memory.

same for the hashtables / arrays.

But if 1000 object would do some action @ the same time then you might have some issue.

best is to narrow down why/when its crashes.
also if its not crashing if you encounter a lot of lag or not.
Also look to the profiler if you see some peaks


  • Playmaker Newbie
  • *
  • Posts: 34
    • View Profile
Re: Optimization when dealing with high numbers of FSMs
« Reply #2 on: January 23, 2021, 05:28:57 PM »
Ah ok cool thats good to know thanks.
All the NPCs are in offset parts of their schedule/FSM, so they are rarely executing the same action at the same time. Probably around 70% at any given time are just in the wait state.
I have a frame rate counter and I don't notice and lag before it crashes. Definitely sounds like I need to learn more about profiling!