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
Angius2mo 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
magikusgierus2mo ago
Okay, I get it. But it would require me to know all the flags from the start, right?
Angius
Angius2mo 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
magikusgierus2mo ago
I guess I already do, I would just have to type them
Angius
Angius2mo ago
You can define them all from the start, yes Or even store them in a .json file and read them on startup
magikusgierus
magikusgierus2mo ago
That's a very interesting snippet, I'll have to look up some things like "sealed" and "record" thanks
Angius
Angius2mo ago
sealed just means it cannot be inherited from
magikusgierus
magikusgierus2mo ago
ah, ok
Angius
Angius2mo ago
And record is a short way of defining a class, with some added nice stuff Like value equality
magikusgierus
magikusgierus2mo ago
and then you create List<Flag> Flags somwhere and add/remove?
Angius
Angius2mo 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
magikusgierus2mo 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
Angius2mo ago
Basically
magikusgierus
magikusgierus2mo ago
ok, thanks