How to calculate Spawnflags! Yes, Spawnflags :o

Hello. I would like to know how to calculate an entity’s spawn flags in order to ent:SetKeyValue ( “spawnflags”, flagnumber ) properly!

flagnum1 + flagnum2 + flagnum3 :smile:

… and flagnum1 is 1, flagnum2 is 2, flagnum3 is 4 and so on?

Presumably. What flags in particular are you trying to set, and to what entity?

To find out if a flag is set do…

[lua]function HasFlag( spawnflags, flag )
return ( spawnflags & flag == flag )

eg, an ent has spawnflags 1326 ( 10100101110 ), and you want to check if the 9th bit ( 2^9 = 256 ) is set you’d do…

[lua]flagSet = HasFlag( spawnflags, 256 )
– returns a bool[/lua]

To set a flag just add it to the current spawnflag value.

I’m pretty sure there’s a tutorial on the wiki covering bitwise operations.

Here you go.

Thanks for your help!

Would be better if you used bitwise or for that.

flag1 | flag2 | flag3

Since we don’t have a bitwise not operator we have to write something to help with that.
local function Not( value )

return -value - 1;


local A = 1;
local B = 2;
local C = 4;
local D = 8;

// flags contains all of the above values
local flags = A | B | C | D;

// remove C
flags = flags & Not( C );


While you can use addition and get predictable results with flags that are powers of two. If you use flags that are not powers of two, then you run into problems. Please don’t use this example in any practical situation, as it will fail horribly. This is just a demonstration.

Lets say we have the following flags.

Lets combine them.

local Value1 = A | B | C | D;
local Value2 = A + B + C + D;

That gives us 15 and 30 respectively. Now lets try and see if a flag is set.

print( Value1 & A );
print( Value1 & B );
print( Value1 & C );
print( Value1 & D );

print( Value2 & A );
print( Value2 & B );
print( Value2 & C );
print( Value2 & D );

That will output the following.

The latter is not the result we were expecting. This is a horrible example and nobody should ever use it in practice. Because it will fail in any other situation. Was just demonstrating how we can get inconsistent results.

Ah, right, I’ll bear that in mind.
However, in this case he wants to calculate what number to put in the ‘spawnflags’ keyvalue of a Source entity.
Since source flags are all powers of 2, all he needs to do is look at the wiki, get the numbers of the spawnflags he wants and add them. :stuck_out_tongue:


(For instance, with a calculator rather than a Lua script)

Yea about that calculator, magic numbers are not a very good programming habit. In the future you’ll come to your code and see 267 and wonder what flags it represents. You’ll then have to go through the list of flags again adding up different combination until you get that value.

You can add and pass them, but please don’t use a magic number, it’ll confuse you when you return to it in the future.

Magic numbers are another reason why I advocate using constants and not the value they represent. I see many people using values like 1 or 3 when they should be using HUD_PRINTTALK, etc.

[lua]ent:SetKeyValue( “spawnflags”, 267 );[/lua]
[lua]ent:SeyKeyValue( “spawnflags”, FLAG1 | FLAG2 | FLAG4 | FLAG9 ); // or addition, whatever floats your boat[/lua]

Which of those would you understand in the future when you return to your code?

I use constants in the place of numerical values where possible in my code, however creating a few constants for a one time operation of say setting a door’s spawnflags seems excessive to me.
I feel
[lua]ent:SetKeyValue( “spawnflags”, 267 );-- FLAG1 | FLAG2 | FLAG4 | FLAG9[/lua]would be more practical, personally.

Okay, so now if you want to add another flag, you’d need to open up your calculator, add the flag’s value, and then put it into the code. With bitwise, you’d simply add | FLAG10 to the code.