This is a method that you find used commonly in C++ (like in the 3dgs sdk for example) but is rarely used in Cscript to my knowledge. Unfortunately, nobody realizes the power that this technique can have. It is used (in a rather advanced form) in weapons.wdl. The common use for this technique is to store several flags in a single number, by assigning each flag a square number. You are likely wondering how this works, and a little about the & operator, so I'll start right into some example code:
Code:
var flaglist;
define _flag1,2;
define _flag2,4;
define _flagA,8;
define _flagZ,16;//all flags get assigned a unique square number
This is the basic setup we work from. Now, to set the flags:
Code:
flaglist = _flag1 + _flagA;
Now your flaglist is equal to 10. Later in the code, we want to find out which flags were put into the original value, _flag1 and _flagA in this case. This is where some blackbox code comes into play (you could work it down as to how it works, but I will not go into the binary equivilents to square decimal numbers). How in the world are we to acheive this?? The user could have put in 2, 1, and 7, how would we know after the calculation? This is why we work only with square numbers. Is there any other combination of square numbers that would equal 10 (without using one twice)? No, there isn't! Now, you can probably now see a complex method of reasoning this out with a lengthly series of IF statements like
Code:
if(flaglist==10) { //then we know that flag1 and flagA were activated... insert appropriate code here
}
This can be tedious and entirely unneccesary. There is a lovely operator called the bitwise-AND which works at a binary level, which I won't go into the technicalities of. The thing is this: we can now test to see if any single flag was in the original addition of flags with one simple statement:
Code:
if( (flaglist & _flag1) != 0)
Note that this is different than using the && operator.
I'm trying to be fairly brief, so, a final analysis can be made from this code snippet that you can play with to your hearts delight.
Code:
define _temp_flags,skill1;
define _transparent,2;
define _flare,4;
define _bright,8;
define _passable,16;
define _shadow,32;
define _user_rotate,64;
//add whatever else you would like for user-defined flags here
function setflagsInEnt()
{
if((my._temp_flags & _transparent) != 0) { my.transparent = on; }
if((my._temp_flags & _flare) != 0) { my.flare = on; }
if((my._temp_flags & _bright) != 0) { my.bright = on; }
if((my._temp_flags & _passable) != 0) { my.passable = on; }
if((my._temp_flags & _shadow) != 0) { my.shadow = on; }
if((my._temp_flags & _user_rotate) != 0)
{
while(1)
{
my.pan += time;
wait(1);
}
}
}
action test_object
{
my._temp_flags = _transparent + _bright + _shadow + _passable +_user_rotate;
setflagsInEnt();
}
I'll make this a little more understandable next time I'm online and can add to it.
EDIT: fixed a little copy/pase error.