Flag: Difference between revisions
No edit summary |
m (→Usage: "Entity flags") |
||
(20 intermediate revisions by 7 users not shown) | |||
Line 1: | Line 1: | ||
A '''Flag''' 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 <code>bool</code> type. | {{LanguageBar}} | ||
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 <code>bool</code> type. | |||
== | Entities in Source commonly use the following flag types: | ||
* Spawn flags - <code>SF_</code> defined in each entity's <code>.cpp</code> file | |||
* Behavior flags - <code>FL_</code> flags defined in [https://github.com/ValveSoftware/source-sdk-2013/blob/master/src/public/const.h#L106 {{Path|public/const.h|icon=cpp}}] | |||
* [[Effect flags]] - <code>EF_</code> flags defined in [https://github.com/ValveSoftware/source-sdk-2013/blob/master/src/public/const.h#L280 {{Path|public/const.h|icon=cpp}}] | |||
* Entity flags (aka <code>EFlags</code>) - <code>EFL_</code> flags defined in [https://github.com/ValveSoftware/source-sdk-2013/blob/master/src/game/shared/shareddefs.h#L577 {{Path|game/shared/shareddefs.h|icon=cpp}}] | |||
Brushes use the following flag types: | |||
* [[Contents flags]] - <code>CONTENTS_</code> flags defined in [https://github.com/ValveSoftware/source-sdk-2013/blob/master/src/public/bspflags.h#L24 {{Path|public/bspflags.h|icon=cpp}}] | |||
* [[Surface flags]] - <code>SURF_</code> flags defined in [https://github.com/ValveSoftware/source-sdk-2013/blob/master/src/public/bspflags.h#L80 {{Path|public/bspflags.h|icon=cpp}}] | |||
==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: | 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: | ||
*<code>Add<*>Flags()</code> | *<code>Add<*>Flags()</code> | ||
*<code>Remove<*>Flags()</code> | *<code>Remove<*>Flags()</code> | ||
*<code>Clear<*>Flags()</code> | *<code>Clear<*>Flags()</code> | ||
*<code>Has<*>Flags()</code> (sometimes <code>Is<*>FlagSet()</code>) | *<code>Has<*>Flags()</code> (sometimes <code>Is<*>FlagSet()</code>) | ||
These are the functions to call for common flag types: | |||
{| class="wikitable" | |||
|- | |||
! | |||
! Prefix | |||
! Add | |||
! Remove | |||
! Check | |||
! Clear | |||
! Get All | |||
! Internal Field | |||
|- | |||
| '''Spawn Flags''' | |||
| <code>SF_</code> | |||
| <code>AddSpawnFlags</code> | |||
| <code>RemoveSpawnFlags</code> | |||
| <code>HasSpawnFlags</code> | |||
| <code>ClearSpawnFlags</code> | |||
| <code>GetSpawnFlags</code> | |||
| <code>m_spawnflags</code> | |||
|- | |||
| '''Behavior Flags''' | |||
| <code>FL_</code> | |||
| <code>AddFlag</code> | |||
| <code>RemoveFlag</code> | |||
| N/A | |||
| <code>ClearFlags</code> | |||
| <code>GetFlags</code> | |||
| <code>m_fFlags</code> | |||
|- | |||
| '''[[Effect flags|Effect Flags]]''' | |||
| <code>EF_</code> | |||
| <code>AddEffects</code> | |||
| <code>RemoveEffects</code> | |||
| <code>IsEffectActive</code> | |||
| <code>ClearEffects</code> | |||
| <code>GetEffects</code> | |||
| <code>m_fEffects</code> | |||
|- | |||
| '''Entity flags''' | |||
| <code>EFL_</code> | |||
| <code>AddEFlags</code> | |||
| <code>RemoveEFlags</code> | |||
| <code>IsEFlagSet</code> | |||
| N/A | |||
| <code>GetEFlags</code> | |||
| <code>m_iEFlags</code> | |||
|} | |||
{{warning|Don't modify the fields in the '''Internal Field''' column directly; always call the functions to add or remove flags, as many of the functions do additional work to notify the entity system that the entity's state has changed.}} | |||
{{note|Make sure you always use the right functions for the right type of flags, as the compiler won't detect if you've made a mistake. It's very easy to mix up flags starting with <code>EFL_</code> and <code>EF_</code>, so always double check. Some of Valve's own code makes these mistakes!}} | |||
'''To operate on several flags at once''' you must use the [[Wikipedia:Bitwise operation|bitwise]] operators <code>&</code>, <code>|</code> and <code>^</code> - the same as the normal boolean operators, but with only one character. | '''To operate on several flags at once''' you must use the [[Wikipedia:Bitwise operation|bitwise]] operators <code>&</code>, <code>|</code> and <code>^</code> - the same as the normal boolean operators, but with only one character. | ||
For instance: | For instance: | ||
<pre> | |||
if ( HasSpawnFlags(SF_CITIZEN_MEDIC | SF_CITIZEN_NOT_COMMANDABLE) ) | |||
... | |||
</pre> | |||
==Creation== | |||
Flags are typically defined as an enum like this: | |||
<syntaxhighlight lang="cpp"> | |||
enum MyFlags_t | |||
{ | |||
FL_MYFLAG = 1 << 0, | |||
FL_MYFLAG = 1 << 1, | |||
FL_MYFLAG = 1 << 2, | |||
// etc. | |||
}; | |||
</syntaxhighlight> | |||
This is the same as: | |||
<syntaxhighlight lang="cpp"> | |||
enum MyFlags_t | |||
{ | |||
FL_MYFLAG = 1, | |||
FL_MYFLAG = 2, | |||
FL_MYFLAG = 4, | |||
// etc. | |||
}; | |||
</syntaxhighlight> | |||
Or: | |||
<syntaxhighlight lang="cpp"> | |||
enum MyFlags_t | |||
{ | |||
FL_MYFLAG = 0x1, | |||
FL_MYFLAG = 0x2, | |||
FL_MYFLAG = 0x4, | |||
// etc. | |||
}; | |||
</syntaxhighlight> | |||
Older code uses the preprocessor to define flags, although this should be avoided as it is not strongly typed: | |||
<syntaxhighlight lang="cpp"> | |||
#define FL_MYFLAG 1<<0 | |||
#define FL_MYFLAG 2 | |||
#define FL_MYFLAG 0x4 | |||
// etc. | |||
</syntaxhighlight> | |||
== Flags in Hammer == | ==Flags in Hammer== | ||
Flags [[Hammer_Object_Properties_Dialog#Flags_Tab|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. | Flags [[Hammer_Object_Properties_Dialog#Flags_Tab|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]], <code>spawnflags</code>. That's what the flags actually are; a specific number determined by the combination of flags that are checked. | ||
{{warning|{{code|spawnflags}} is an signed int in-game, but an unsigned long in Hammer!}} | |||
[[Category:Flags]] | [[Category:Flags]] |
Latest revision as of 20:24, 24 July 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 (aka
EFlags
) -EFL_
flags defined ingame/shared/shareddefs.h
Brushes use the following flag types:
- Contents flags -
CONTENTS_
flags defined inpublic/bspflags.h
- Surface flags -
SURF_
flags defined inpublic/bspflags.h
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()
)
These are the functions to call for common flag types:
Prefix | Add | Remove | Check | Clear | Get All | Internal Field | |
---|---|---|---|---|---|---|---|
Spawn Flags | SF_
|
AddSpawnFlags
|
RemoveSpawnFlags
|
HasSpawnFlags
|
ClearSpawnFlags
|
GetSpawnFlags
|
m_spawnflags
|
Behavior Flags | FL_
|
AddFlag
|
RemoveFlag
|
N/A | ClearFlags
|
GetFlags
|
m_fFlags
|
Effect Flags | EF_
|
AddEffects
|
RemoveEffects
|
IsEffectActive
|
ClearEffects
|
GetEffects
|
m_fEffects
|
Entity flags | EFL_
|
AddEFlags
|
RemoveEFlags
|
IsEFlagSet
|
N/A | GetEFlags
|
m_iEFlags
|


EFL_
and EF_
, so always double check. Some of Valve's own code makes these mistakes!
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) ) ...
Creation
Flags are typically defined as an enum like this:
enum MyFlags_t
{
FL_MYFLAG = 1 << 0,
FL_MYFLAG = 1 << 1,
FL_MYFLAG = 1 << 2,
// etc.
};
This is the same as:
enum MyFlags_t
{
FL_MYFLAG = 1,
FL_MYFLAG = 2,
FL_MYFLAG = 4,
// etc.
};
Or:
enum MyFlags_t
{
FL_MYFLAG = 0x1,
FL_MYFLAG = 0x2,
FL_MYFLAG = 0x4,
// etc.
};
Older code uses the preprocessor to define flags, although this should be avoided as it is not strongly typed:
#define FL_MYFLAG 1<<0
#define FL_MYFLAG 2
#define FL_MYFLAG 0x4
// etc.
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.
