Flag: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
m (link)
No edit summary
Line 1: Line 1:
A '''Flag''' is a [[boolean]] value that is stored in an [[integer]] variable.
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.
 
Flags are stored together within an integer instead of a boolean for each flag because the smallest amount of memory that a program can access is a byte. If you used a boolean for the flags you'd be using a whole byte (8 bits) when all you need is 1 bit. An integer contains 32 bits. By placing the flags within an integer it means that the space is used much more efficiently than if you used a lot of booleans. Up to 32 individual flags can be stored in a single int, since each is represented by one of its bits.


== Creation ==
== Creation ==


Flags are created like this:
Flags are typically defined like this:


  #define FL_MYFLAG 1<<0
  #define FL_MYFLAG 1<<0
Line 12: Line 10:
  etc.
  etc.


Or sometimes like this (e.g. [[spawnflag]]s):
This is the same as:


  #define FL_MYFLAG 1
  #define FL_MYFLAG 1
Line 18: Line 16:
  #define FL_MYFLAG 32
  #define FL_MYFLAG 32
  etc.
  etc.
You can then use the #defs in place of the numbers.


== Usage ==
== Usage ==


Flags are managed through functions, never directly. There are several different types of flags and each has its own set, 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>
Line 37: Line 33:
  ...
  ...


==Flags in Hammer Editor==
== 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.



Revision as of 09:36, 15 September 2011

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 bool type.

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 16
#define FL_MYFLAG 32
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() (sometimes Is<*>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.