Sequence State System

Building a sequence state system is an absolute must for keeping in game sequences manageable.  A sequence is usually a scripted event that the player is not in control of.  It could be a full screen animation or an in game event.   Having this type of system allows you to start or end a sequence at any moment.

For example, at one point in the demo level an alert is sounded.  A red light flashes and Ammut and Tatenen ask each other what it is about.  After deciding they are not sure they begin to walk toward the bridge while the scene fades out and Ammut excitingly shouts “Alllarrrrmmm!!”.    The player is not in control and this is considered a scripted sequence. Another sequence is when we first see Ammut the camera shakes (with sound effects) and Ammut says he should get to the engine room.

Much of a sequence can be handled through XML.  The XML file will dictate the order that events in the sequence happen and it will also contain the dialogue for that sequence.
Here is a simple XML sequence.

			Use hand on Ammuts bed
			I'll just take a quick nap.
			start the nap...fade out...wait a few seconds!
			End the nap and fade back in
			Ahhh, much better.

In this sequence the player clicked Ammut’s bed with the Hand icon.  Ammut first says he will take a nap and then there are two actions.  The First action starts the nap (the camera fades out) and the second ends the nap(the camera fades back in).  These could easily be combined into one action but I wanted to show how you can have any number of actions.  Ammut then says he feels better.

The custom XML editor has a list of available characters for the specific scene to choose from so there is no need to worry about spelling errors and such.

The code handling the sequence consists of a sequence controller which keeps a dictionary (.net hashtable like array) of all the sequences that need their own script file.  If a sequence is simple enough, it can solely exist in the XML, if the sequence needs more complicated behavior it gets its own sequence script file.

The controller script has a lot to it but here is the method where the specific sequence is run.  The sequence name is a string because it comes from the XML file.

IEnumerator SequenceCoRoutine (string seq, int stateInput)
 currentSequence = seq;

 _sequenceRunning = true;

 yield return StartCoroutine(CheckForNoActions(seq));

 if (containsAction)
 yield return StartCoroutine(sequenceDictionary[seq].PerformSequence(stateInput));

 _sequenceRunning = false;

If the sequence contains no actions there is a method that will display the dialogue from the XML.  If there are actions, that sequences script file is run.

The sequence script file inherits from an abstract class that contains a startsequence method and endsequence method.  Dialogue from the XML is automatically spoken, and any specific action gets its own method that will be called and yielded to.  After all the actions and dialogue are done the endsequence method is run and the sequence ends.

Hopefully this will get you going on building a sequence state system.  If you’d like more details i’d be happy to post more about the system i’m using!

This entry was posted in DevBlog. Bookmark the permalink.

8 Responses to Sequence State System

  1. pat says:

    Hey, great stuff! Absolutely rocks!! Btw, i’m kinda lost now, i’m not sure about when each routine takes place …I guess that some kind of flow chart to clarify the main logic of the game should help at this point…what do you think. I know it’s too much to ask, but in the other hand you will have a great book to publish at the end! Thank you so much, Mike!

  2. admin says:

    Hi Pat,
    Man i’m sorry I haven’t been more clear with my posts! I was thinking of them more as inspirational guides, but I will try hard to make a much more outline friendly post next time that gives more of a big picture of how everything fits together. After that maybe these posts will make more sense!

  3. pat says:

    Please don’t get me wrong, your post are exactly what they should be, please continue like this, but i guess that a few clues about how the ‘whole structure’ (for one room cycle, perhaps?) works will be really of help. Thanks. Great idea about supporting the Ouya and maybe Gamestick also! What about touch controls for apple devices?
    Keep it rocking!

  4. sarah says:

    Thank you very much for your dev posts! They are great for beginners like me to get started with programming a point & click using Unity. Keep up the great work! 🙂

  5. Brett M says:

    Is there a reason you’re using XML instead of using the Hierarchy? You can have a Sequence GameObject with Actions as its children.

    • Mike Pinto says:

      Hi Brett,
      There are certainly many ways to have a sequence system work in Unity. You could use scriptable objects as sequences instead of xml. I am not sure I feel storing sequences as children gameobjects of a manager is a good idea. Since a sequence has no need for a “world” presence or transform, it is best stored in an array on the sequence manager. Is there a reason you would want them as Gameobjects?

      Xml is a way to make your data portable. If you wanted to use a custom editor for sequences outside of Unity, you could. If you wanted to give dialogue to be translated, you could make a custom editor that only allows dialogue to be edited, but still shows the other sequence events for context.
      If you use diff, you can visually see what has changed in the xml between each version. If they were Unity gameobjects there would be no easy way to see the difference between checked in versions.
      If you alter too much of the class and you are storing the data in Unity gameobjects you could risk breaking the serialization and loosing all your data, but if it is xml based, there is no risk of losing data if you decide to change the sequence class.

      Another reason also against the hierarchy is that it would be very cumbersome to manage. It is better to have a custom editor so you can sort and control the data instead of having it sitting out in the hierarchy where it could be quite easily moved/deleted with a mistaken keystroke.

      hope that helps!

      • Brett M says:

        You’re right about them not really needing a “world” presence, but I’m not a great programmer, so I was doing it just so I could use them in the inspector, and use that as a kind of editor.

        I’ll look into learning how to work with XML with unity.

        • Mike Pinto says:

          I see, maybe I should make a post about custom editor windows. Those are unity editor scripts that allow you to create editor windows for custom scenarios like XML editing.

          You should look up those or Scriptable Objects. Scriptable Objects might be easier to work with thank building XML parsers if you are new to programming.

Leave a Reply

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