Categories
Virtual World Building

Llama Drama Dev Blog – Llama State Machine

This thing has given me so much trouble over the months. A previous project I did also had a state machine so I thought I had learnt somewhat from that one. Apparently not. I struggled with this thing. What started as four states and a bunch of item interactions quickly became nine states with numerous conditions between them. It hurt my brain to think about it so I spent a few months just writing it down on paper with pseudocode so that I could change things easily but still see previous work.

It was also really easy for me to get confused when writing the script to the point where it was easier for me to just start a new script than to try figure out what is missing from the current one, as shown by the numerous similarly named scripts in the adjacent image.

The most confusing part of making the state machine was figuring out how to do it. Having only made a very simple one before, my immediate thought was call each state as separate functions in the update function. I then got confused about how I would transition between each state and just everything. The other idea was to put absolutely everything in the update function but I figured that there would be a lot and putting it all in the update function might not go very well. It would also mean I’d have to plan around the function getting called every frame, and I couldn’t just make the code wait before executing the next line. The other option I was shown was to make classes for each state and fill the classes with the code for each state. I tried each of them and got confused every time. I thought making separate classes for each of them would be the best way to go, if only I could wrap my head around it. I understood it in class but then I went home to try and just couldn’t understand anything. So that’s a no go. I couldn’t figure out how I would go about having each state as a function, so that was out. Leaving me with putting everything in the update function.

States as Classes
States as Functions
Everything in Update()

The state machine ended up with ten states, nine of which are currently in use. Idle, Searching, Advancing, Retreating, Annoying, Attacking, Spooked, Stunned, Trapped, and Grabbed. Grabbed is not in use currently as we decided to leave it out, but it would be for when the player picks up the llama. Idle is when it’s doing nothing and is used as a transitional state. Searching is when it gets a random target to go to. Advancing is when it paths to its target. Retreating is when it runs away and paths to a random hiding spot to then check if there is a line of sight with the player before teleporting away. Annoying is the task version of attacking – the llama takes one of the items required for the player to complete a task. Attacking is for the llama to do some sort of damage to the player or specific items from the fabricator. Spooked is when a thrown item or a specific fabricator item effect makes the llama run away but come back to the same task target or fabricator item target. Stunned keeps the llama from doing anything until the duration is over. Trapped is similar to stunned only it requires a physical trap object to keep the llama there.

There’s not much else to say about the state machine. Despite how big and important it is, it’s pretty much just the states, how to transition between the states, and any interactions with other objects in the game.

Leave a Reply

Your email address will not be published. Required fields are marked *