Flags, State and Key Items

My goal this weekend was to implement the concept of flags and state into the game.

I was using a system of flags for keeping track of which chests were opened, but nothing else. I decided to scrap that system, and migrate it and other pieces of state over to a new system.

I’ve included the indecipherable scribblings of a madman for your reference:

So what we have here is…

We have three tables, one for NPC (non player characters), one for Dialog, and one for Flag. NPCs carry an array of NPCDialogs, each of which hold a reference to an id in the Dialog table. The NPCDialog has an array of Flag ids, which are tied to flags in the Flag table. The NPC, when interacted with, will then say the dialog that is appropriate to the game state, as dictated by the flags that have their flagged property set to True. The cool thing is that NPCDialogs aren’t just tied to a single flag, they can also rely on multiple flags being flagged. We reduce to find the dialog that best corresponds to the current game state:

  private getCurrentDialog() {
    const sm = State.getInstance()
    const dialog = this.dialog.reduce((acc, dialog) => {
      if (sm.allAreFlagged(dialog.flags)) {
        acc = dialog;
      }
      return acc;
    });
    return dialog.message;
  }

So for example, when the game starts, Lo doesn’t have her hearing aids. When she talks to Ryan, she can’t hear him. But when she gets the hearing aids on the table the flag for getting hearing aids is flipped to true, and the Ryan NPC object now knows what dialog to say in response. Here it is in action:

An actual exchange that happens every day of our lives.

The potential that this opens up for the game is actually pretty grand. Now we can dictate game state with a really granular system of flags. I hope to expand this system to apply to item presence, NPC position and other things. This is one of the key parts of the game I needed in place in order to complete the loop.


Key items were also introduced this weekend. Collecting the key item will flip a flag, and as seen above, cause other NPCs to change their dialog in the world. Pretty cool!

I also implemented an item preview functionality that adds a panel for seeing a small preview of the focused item in the UI.

This week, I think I want to start building out the first dungeon. It’ll be a simple affair. I want to implement a few floors, some treasure, and a final boss. I think it’s doable!

Leave a comment