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
Angius
Angius7mo ago
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)
magikusgierus
magikusgierusOP7mo ago
Okay, I get it. But it would require me to know all the flags from the start, right?
Angius
Angius7mo ago
Example:
public sealed record Flag(string Name, bool PreventsJumping, bool AllowsFlying)
{
public static readonly Flag Fighting = new(nameof(Fighting), true, false);
public static readonly Flag NoSaveZone = new(nameof(NoSaveZone), true, false);
public static readonly Flag Creative = new(nameof(Creative), false, true);
}
public sealed record Flag(string Name, bool PreventsJumping, bool AllowsFlying)
{
public static readonly Flag Fighting = new(nameof(Fighting), true, false);
public static readonly Flag NoSaveZone = new(nameof(NoSaveZone), true, false);
public static readonly Flag Creative = new(nameof(Creative), false, true);
}
magikusgierus
magikusgierusOP7mo ago
I guess I already do, I would just have to type them
Angius
Angius7mo ago
You can define them all from the start, yes Or even store them in a .json file and read them on startup
magikusgierus
magikusgierusOP7mo ago
That's a very interesting snippet, I'll have to look up some things like "sealed" and "record" thanks
Angius
Angius7mo ago
sealed just means it cannot be inherited from
magikusgierus
magikusgierusOP7mo ago
ah, ok
Angius
Angius7mo ago
And record is a short way of defining a class, with some added nice stuff Like value equality
magikusgierus
magikusgierusOP7mo ago
and then you create List<Flag> Flags somwhere and add/remove?
Angius
Angius7mo ago
You could do a class as well
public sealed class Flag(string name, bool preventsJumping, bool allowsFlying)
{
public string Name { get; } = name;
public bool PreventsJumping { get; } = preventsJumping;
public bool AllowsFlying { get; } = allowsFlying;

public static readonly Flag Fighting = new(nameof(Fighting), true, false);
public static readonly Flag NoSaveZone = new(nameof(NoSaveZone), true, false);
public static readonly Flag Creative = new(nameof(Creative), false, true);
}
public sealed class Flag(string name, bool preventsJumping, bool allowsFlying)
{
public string Name { get; } = name;
public bool PreventsJumping { get; } = preventsJumping;
public bool AllowsFlying { get; } = allowsFlying;

public static readonly Flag Fighting = new(nameof(Fighting), true, false);
public static readonly Flag NoSaveZone = new(nameof(NoSaveZone), true, false);
public static readonly Flag Creative = new(nameof(Creative), false, true);
}
or something to that effect
magikusgierus
magikusgierusOP7mo ago
So it's more advance version of doing something like
enum SavingFlag
{Fighting, Location, Cutscene}

HashSet<SavingFlag> SavingFlags;

bool CanPlayerSave()
{
return SavingFlags.Count == 0;
}
enum SavingFlag
{Fighting, Location, Cutscene}

HashSet<SavingFlag> SavingFlags;

bool CanPlayerSave()
{
return SavingFlags.Count == 0;
}
but has the added ability to not only prevent saving, but modify other things?
Angius
Angius7mo ago
Basically
magikusgierus
magikusgierusOP7mo ago
ok, thanks
Want results from more Discord servers?
Add your server