Flag: Difference between revisions
(Add list of common flag types) |
(Add links to cpp files for each flag type) |
||
Line 3: | Line 3: | ||
Entities in Source commonly use the following flag types: | Entities in Source commonly use the following flag types: | ||
* Spawn flags - <code>SF_</code> | * Spawn flags - <code>SF_</code> defined in each entity's <code>.cpp</code> file | ||
* Behavior flags - <code>FL_</code> | * Behavior flags - <code>FL_</code> flags defined in [https://github.com/ValveSoftware/source-sdk-2013/blob/master/mp/src/public/const.h#L106 {{Path|public/const|h}}] | ||
* [[Effect flags]] - <code>EF_</code> | * [[Effect flags]] - <code>EF_</code> flags defined in [https://github.com/ValveSoftware/source-sdk-2013/blob/master/mp/src/public/const.h#L280 {{Path|public/const|h}}] | ||
* Entity flags ("EFlags") - <code>EFL_</code> | * Entity flags ("EFlags") - <code>EFL_</code> flags defined in [https://github.com/ValveSoftware/source-sdk-2013/blob/master/mp/src/game/shared/shareddefs.h#L577 {{Path|game/shared/shareddefs|h}}] | ||
==Creation== | ==Creation== |
Revision as of 07:02, 25 January 2025
A Flag (bitmask or mask) is a boolean value that is stored within a large variable. This is advantageous because the smallest amount of memory that a program can allocate is 1 byte (or 8 bits), but a boolean only strictly needs 1 bit. If you have a lot of booleans to store, flags are eight times more efficient than the dedicated bool
type.
Entities in Source commonly use the following flag types:
- Spawn flags -
SF_
defined in each entity's.cpp
file - Behavior flags -
FL_
flags defined inpublic/const.
h
- Effect flags -
EF_
flags defined inpublic/const.
h
- Entity flags ("EFlags") -
EFL_
flags defined ingame/shared/shareddefs.
h
Creation
Flags are typically defined like this:
#define FL_MYFLAG 1<<0 #define FL_MYFLAG 1<<1 #define FL_MYFLAG 1<<2 etc.
This is the same as:
#define FL_MYFLAG 1 #define FL_MYFLAG 2 #define FL_MYFLAG 4 etc.
Usage
Flags should be managed through Source's dedicated functions, never directly. There are several different types of flags and each has its own set of functions, but they all follow the same naming pattern:
Add<*>Flags()
Remove<*>Flags()
Clear<*>Flags()
Has<*>Flags()
(sometimesIs<*>FlagSet()
)
To operate on several flags at once you must use the bitwise operators &
, |
and ^
- the same as the normal boolean operators, but with only one character.
For instance:
if ( HasSpawnFlags(SF_CITIZEN_MEDIC | SF_CITIZEN_NOT_COMMANDABLE) ) ...
Flags in Hammer
Flags appear in object properties in Valve Hammer Editor. Different objects will have different flags available. A flag appears as a checkbox, meaning each flag can be either on or off. You can also access flags as a keyvalue, spawnflags
. That's what the flags actually are; a specific number determined by the combination of flags that are checked.
