Nam of this pattern?
Let's say I'm creating a game. I want to prevent player from saving in some situations, eg.:
- during fights
- in specific location
- in other cases
I could just create variable canPlayerSave on SaveSyste and let other systems set it. But what if:
- player enters specific location - variable is set to true
- fight start - variable set to true
- fight end - variable is set to false, even though player is still in the non-saving location
Solution I worked out: instead of bool, use int: I created a canPlayerSaveCounter, and every time player starts a fight or enter a non-saving location, it's increased by one, and decreased when it ends. Player can only save when the counter is exactly 0.
My question is: does the thing I invented has a name? Can it be implemented differently?
14 Replies
I'd probably use some sort of a tag/flag system
Have a list of player flags and add
Flags.Fighting
when fight starts, remove it when it ends
Same for other possible states
The flags can have a bool like PreventsSaving
And save system can check if Flags.Any(f => f.PreventsSaving)
Okay, I get it. But it would require me to know all the flags from the start, right?
Example:
I guess I already do, I would just have to type them
You can define them all from the start, yes
Or even store them in a
.json
file and read them on startupThat's a very interesting snippet, I'll have to look up some things
like "sealed" and "record"
thanks
sealed
just means it cannot be inherited fromah, ok
And
record
is a short way of defining a class, with some added nice stuff
Like value equalityand then you create List<Flag> Flags somwhere and add/remove?
You could do a class as well
or something to that effect
So it's more advance version of doing something like
but has the added ability to not only prevent saving, but modify other things?
Basically
ok, thanks