Rob - Flag System for Total Narrative Control - Senior Production Blog #7

What is the Flag System?

I realized that a huge system I worked into Frog Snatchers hasn't received proper coverage in one of these posts yet. In order to understand how we're handling pretty much all the progress the player makes, you don't have to look any further than the Flag System. A Flag System can be a few different things, but the one I originally prototyped (and then vastly upgraded this semester) is just keeping track of a global list of booleans. When something important happens, we set its flag to true (or false, potentially), and every object in the game can tell that the event occurred. Clear cut examples of this are how every frog has a flag that activates when you get them, every ability has a flag so you know when you unlock them, and every story-based event has at least one flag so we can tell exactly where the player is in the story. Without this system, it'd be nearly impossible to deliver a believable narrative experience to a player in a non-linear game, as progress is different from player to player, and needs to be tracked in small increments,

 Flags represented by a Name and a Bool, shown at runtime. They are editable from this window for debugging purposes!

Flags represented by a Name and a Bool, shown at runtime. They are editable from this window for debugging purposes!

Implementing the System

The script I wrote for generating our Flag System is potentially the biggest workaround solution in any script in Frog Snatchers. While creating a globally accessible list of bools is normally an easy task, it was more difficult than I anticipated to accomplish that task with these 2 self-imposed conditions:

  1. The flags all need specific and unique names to describe their task (for organization and collaboration)
  2. The flags should be able to be chosen from a drop-down menu, instead of having to type out their name (to remove the possibility of typos)

To make a long story short, I decided that the best way to represent these flags to the team would be in making them all part of an Enum. Enums are naturally selected from drop-down menus in Unity, which means I don't need to create a custom inspector for every single script that simply wants to reference a flag in some way. Unfortunately, Enum members aren't editable like strings in a list. That being said, thanks to the StreamWriter in Unity, I was able to take in the names of the Enums as strings from anyone who wants to add one in my EditorWindow, and then I can rewrite an entire script called "FlagEnum.cs" and resave it. Then, the project quickly recompiles, and we have an updated Enum with all of the entries in it. I've never written a script that can write other scripts before, but it absolutely works and just needs more error checking before it could become a reusable system.

 This is the editable list of flags that will turn into an Enum when you press "Save"

This is the editable list of flags that will turn into an Enum when you press "Save"

 This is the part of the script that occurs when you press "Save". As you can see, it literally just writes the script from the next image!  This was directly inspired by this UnityAnswer:   https://answers.unity.com/questions/1170350/editorscript-generate-enum-from-string.html

This is the part of the script that occurs when you press "Save". As you can see, it literally just writes the script from the next image!

This was directly inspired by this UnityAnswer: https://answers.unity.com/questions/1170350/editorscript-generate-enum-from-string.html

EnumOutput.PNG

The only other thing to note is that the actual list of Bools we use to track game progress is generated when the game starts, based on how long that Enum is. We can't add or remove flags at runtime; we can only change the Bools that represent them.