GMWolf - Defining States with Objects

Game Maker State Machines often end up being very large objects, with a seemingly endless number of lines in each Event. I wanted to approach the creation of State machines using the powerful dynamic functions GameMaker provides, in order to make better use of the Object Resource.

The Idea

The Basic Idea is to have a separate object for each State the state machine can be in.
Such a state object would have all the behaviour associated with that state, in each of the respective events as you would program any other object. The code can reference variable just as if you were inside the main object.

The State Machine object stores the Object index of the current state in a 'state' variable, and has each of the events found in the state object call the event from the state object using event_perform_object.

Event_perform_object is a great function that calls the code from another object's event, within the currently executing instance. This is great to get 'inheritance' like systems when you don't know at compile time what object you are inheriting the event from. This is such a case.

This system has the advantage of being incredibly flexible, compared to a monolithic approach.
States can be reused and shared between state machines.
States are also easier to identify and work with, as they are self-contained, making it easier to follow the logic.

However, The issue remains that variables must be shared across all states, which can mean trouble when dealing with very large, or otherwise complex state machines.
This is why I propose yet another alternative: The Action actor model.
I have already made a video on such a system, which you can find here, but I will also be posting a new blog post explaining the system in more detail and explaining why the small performance cost associated with more instances is mostly irrelevant for most use cases.

For now, I hope you learnt at least something from this post, if not simply a new way of creating state machines. If you have any questions or ideas I would love to discuss them in the comments, which you can find below!


Popular posts from this blog

GMStruct - extending GML with code generators

GMStruct Inheritance and Types