Tracking whether actions have taken place already

Hello,

I finished my first early release of my first game a few weeks ago. It was the first time I made an actual UI that prompted up to the user based on things they did through the game (like a tutorial initially). I did this by using a whole load of boolean fields and storing them in a LevelManager script and then accessing them from various other scripts across the game when certain things occurred (e.g. player moves within a collider of a planet, it checks whether the boolean flag for “first time in the planet” is false and then does something).

So all these booleans look like this:

    public bool firstTimeWithinRangeOfMotherhship = false; // first time the player moves
    public bool seenMothership = false; // track whether the ship has been seen before
    public bool seenFirstResourcePlanet = false; // track whether ship has seen planet before
    public bool seenFirstOrbitalMinerPlanet = false; // as above!
    public bool firstOrbitalMinerIsMining = false;
    public bool resourcesToSaveShipCollected = false;
    public bool firstDialogueCompleted = false;
    public bool firstMinerBuilt = false;

It all worked fine, but it just didn’t exactly feel like a very neat way to do it. And the use of the variables was spread across all the other scripts so I could see maintenance being a pain.

It’s obviously a very common thing to need to do, to trigger things based on a player doing something for the first time and then not again when it does it a second time… so I just thought I’d check on here whether there is a better way to do this kind of thing.

Thanks in advance.

Looks pretty tidy to me! The benefit of putting it all in one structure is you have a leg up on loading/saving your game state: just JSON serialize the object they’re in and write that string to disk!

I would also add that your variable names are spot-on. If I was looking at this code and you weren’t around, I don’t think I’d have a hard time figuring out what controls what.

1 Like

thanks Kurt, I’ve not actually implemented saving before! but that makes a lot of sense yeah, I guess you don’t then need to go around storing every single object. Thanks!

These could be marked static (because they are game-wide values), they’re then accessible anywhere without needing to manually “get” the script they’re in from the calling script (with GetComponent etc). Also they would persist across scenes.

True… but then you cannot serialize them with JSON to save/load them. :slight_smile:

Also, this is my go-to for super-simple Unity singleton:

Simple Singleton (UnitySingleton):

Some super-simple Singleton examples to take and modify:

Simple Unity3D Singleton (no predefined data):

Unity3D Singleton with Prefab used for predefined data:

These are pure-code solutions, do not put anything into any scene, just access it via .Instance!

If you want to save even more typing, shorten Instance to I