CUtlFlags

From Valve Developer Community
Jump to navigation Jump to search
Dead End - Icon.png
This article has no Wikipedia icon links to other VDC articles. Please help improve this article by adding links Wikipedia icon that are relevant to the context within the existing text.
January 2024

CUtlFlags is a simple class that makes the use of flags easier. Code can be found in public/tier1/utlflags.h.

Setting flags

Setting a flag can be done using the SetFlag method, which takes a bitmask and the option to turn the flag on or off.

// Example of a flag enum
enum FlagEnum_t
{
	FLAG_ONE = 0x0001,
	FLAG_TWO = 0x0002,
	FLAG_THREE = 0x0004,
	FLAG_FOUR = 0x0008,
};

// Initialize flag class
CUtlFlags< unsigned short > flags;

// Turn on FLAG_ONE and FLAG_TWO
flags.SetFlag( FLAG_ONE );
flags.SetFlag( FLAG_TWO );

// Turn off FLAG_TWO
flags.SetFlag( FLAG_TWO, false );

Clearing flags

Just as easily, you can clear a flag using SetFlag( FLAG, false ) or the more succinct ClearFlag( FLAG ):

// Clear FLAG_ONE
flags.ClearFlag( FLAG_ONE );

You can also clear all flags:

// Clear all flags
flags.ClearAllFlags();

Querying flags

Using IsFlagSet( FLAG ) and IsAnyFlagSet(), you can check if a certain flag is set:

// Clear all flags
flags.ClearAllFlags();

flags.IsFlagSet( FLAG_ONE ); // Returns false

flags.SetFlag( FLAG_TWO );

flags.IsAnyFlagSet(); // Returns true