Trimming The Fat With Playmaker – Spaghetti Edition
I had to share this update because it’s kind of wacky, and shows what you can get up to with Playmaker.
The way I was approaching things, I would have had to make a different prefab for each NPC type because the FSM is attached to the object, and certain NPCs would need slightly different behavior. For example, merchants will dock and jump, but police need to just know they entered a trigger so they can turn to their next node.
For some reason I thought that was kind of unnecessary to have individual prefabs, since the core script was modified to work with all NPC types. With the script being so accommodating but the FSM being specific to the prefab, I would end up confusing myself (which I know, because I already did, and that was only with two prefabs!).
So I thought: why not set a property in the script so I could define the type of NPC, and then automatically have their behavior change based strictly on that setting? That’s technically how I had the original movement script, and that was cool, but since I was working with Playmaker, I needed to change the FSM to match the more generic application.
The bulk of the changes are applied to the Move state. When the NPC enters a trigger (station or jumpgate), we need to transfer the state to the node of the appropriate type. In the Station and Jumpgate nodes, there’s a property checking Action that looks at the “NPCType” setting from the script. It fires an event based on the value of that property, which is why you see the different NPC types attached to the Station and Jumpgate events. Each NPC type event routes the action to the appropriate next state. For merchants, a jumpgate will destroy the object, but for the police, it just routes to the Select Next state. Similarly, merchants will route to the Dock state when they encounter a station trigger, and the police route to the Select Next node as usual.
It looks messy, but it worked great, and there was practically no modifications needed to the underlying script, aside from adding the “NPCType” property (an enum) and a special translation property “NPCTypeForPM”, because Playmaker can’t recognize enums as a valid property type for reading purposes.