• How to calculate Spawnflags! Yes, Spawnflags :o
    13 replies, posted
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?
[QUOTE=deluvas;19062647].. and flagnum1 is 1, flagnum2 is 2, flagnum3 is 4 and so on?[/QUOTE] 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 ) end[/lua] eg, an ent has spawnflags [b]1326[/b] ( 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. [url]http://wiki.garrysmod.com/?title=Bitwise[/url]
Thanks for your help!
[quote=lexic;19062532]flagnum1 + flagnum2 + flagnum3 :smile:[/quote] Would be better if you used bitwise or for that. [code]flag1 | flag2 | flag3[/code] Since we don't have a bitwise not operator we have to write something to help with that. [lua] /*------------------------------------ Not() ------------------------------------*/ local function Not( value ) return -value - 1; end 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 ); [/lua]
[QUOTE=Jinto;19074368]Would be better if you used bitwise or for that. [code]flag1 | flag2 | flag3[/code][/QUOTE] Why?
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. [list] [*]1 [*]5 [*]9 [*]15 [/list] Lets combine them. [code]local Value1 = A | B | C | D; local Value2 = A + B + C + D;[/code] That gives us 15 and 30 respectively. Now lets try and see if a flag is set. [code]print( Value1 & A ); print( Value1 & B ); print( Value1 & C ); print( Value1 & D );[/code] [code]print( Value2 & A ); print( Value2 & B ); print( Value2 & C ); print( Value2 & D );[/code] That will output the following. [quote]1 5 9 15[/quote] [quote] 0 4 8 14[/quote] 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. :P [editline]03:41PM[/editline] (For instance, with a calculator rather than a Lua script)
[QUOTE=Lexic;19074918]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. :P [editline]03:41PM[/editline] (For instance, with a calculator rather than a Lua script)[/QUOTE] 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.
[QUOTE=Lexic;19075498]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.[/QUOTE] 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.
Sorry, you need to Log In to post a reply to this thread.