How do I use Bitflags

Arkless

New Member
Reaction score
31
I don't understand how to use bitflags (like in the GUI) within galaxy code.

Converting it to script (with the GUI), the type of my bitflag preset variable is integer. But using integers I can't use the given syntax for it (it says wrong type).
The syntax is the following:
MyBitflag = 0x00000001 | 0x00000002 | 0x00000004 | ... | 0x80000000; // for the different flags

if (MyBitflag == 0x00000001 | 0x00000008) {
DoSomething();
}



It's not hard to do that yourself, thought I prefer the version abouve.
My version would be:
ModI(Int/<2^(Flag-1)>,2) = 1 //== to check if a flag was set
Int = Int + <2^(Flag-1)> //== to set a flag
 

eXirrah

New Member
Reaction score
51
you must use Custom Script. The And operator is & and the Or operator
is |. To check if a flag is set in a range of flags use &.

For example you have flags:
flag 1, flag 2, flag 3

a function that will let you choose which flags to set and the variable
that holds the flags is called flags

if you want to check if flag 1 is set in flags then you do this
Code:
if (flags & flag 1) then
{
    the flag is set;
} else {
    the flag is not set;
};
I had the same problem yesterday and created a custom function


Code:
Compare Flags
    Options: Function
    Return Type: Boolean
    Parameters
        Flags = (Integer(Preset)) <Integer>
        Value = (Integer(Preset)) <Integer>
    Grammar Text: Compare Flags(Flags, Value)
    Hint Text: (None)
    Custom Script Code
    Local Variables
    Actions
        General - Custom Script: 
                if (lp_flags & lp_value) {
                    return true;
                };
                return false;

this function checks if Value flag is set in Flags flags.

Check out this library as well : Flag Operators Library
 

Arkless

New Member
Reaction score
31
My problem is something else ^^
I don't get why I can't use & | << >> ~ ^ operators at all. Whenever I do that I get a compiler error. I even tried to convert a GUI bitflag to galaxy script but it uses an integer as well.
 

eXirrah

New Member
Reaction score
51
Seems like bitwise operations in sc2 return integer, so you can't use
(My_Flags & 0x01 == true). You cannot compare integer to boolean but
seems like:
Code:
if (My_Flags & 0x01) {
    DoSomething()
}
works just fine.

I think that bitwise operators operate with integers and bitflags are presets.
You may need to convert bitflags to integers before performing operations on them.
 

Arkless

New Member
Reaction score
31
Seems like bitwise operations in sc2 return integer, so you can't use
(My_Flags & 0x01 == true). You cannot compare integer to boolean but
seems like:
Code:
if (My_Flags & 0x01) {
    DoSomething()
}
works just fine.
I know how bitmanipulation operators work, I just got a type error when I tried "if (Int == 0x00000001 | 0x00000008) {". It won't even let me gui lines, I get an error saying wrong type pointing on the line.
( | bit or)


edit:
Code:
if (My_Flags & 0x01) {
    DoSomething()
}

That works? Thats ridiculous, it should should return an integer as well:
3 & 5 = 1
0011 & 0101 = 1
 

Vestras

Retired
Reaction score
248
If you want to check if a bitwise flag is true, atleast in C#, you do this:
Code:
if ((comparisonVar & 0x10) != 0) {
 

eXirrah

New Member
Reaction score
51
You should check the library I posted link to in my first post.

There are functions and stuff that operate with bitflags, read the code
and you should understand how this works.
 

Arkless

New Member
Reaction score
31
You should check the library I posted link to in my first post.

There are functions and stuff that operate with bitflags, read the code
and you should understand how this works.

I did, it was nothing new to me, it still doesnt work. Gonna reinstall soon, the error is kinda weird (wrong type doesn't really fit there).
 

eXirrah

New Member
Reaction score
51
I get wrong type when I use (My_Flags == 0x01 | 0x02) too. It's not your
editor, it's just how galaxy script works. I started using similar statement
and then tested it all night wondering why it doesn't work until I found that
library and noticed that (My_Flags & 0x01) and (My_Flags & 0x02) is how it
must be in galaxy....

I think there is nothing you can do to change that...

If you do though, let me know.
 

Arkless

New Member
Reaction score
31
I get wrong type when I use (My_Flags == 0x01 | 0x02) too. It's not your
editor, it's just how galaxy script works. I started using similar statement
and then tested it all night wondering why it doesn't work until I found that
library and noticed that (My_Flags & 0x01) and (My_Flags & 0x02) is how it
must be in galaxy....

I think there is nothing you can do to change that...

If you do though, let me know.

k, thx, that's an answere that helped me ^^ (at least now I know that it was no error on my side :p)
 

Arkless

New Member
Reaction score
31
I just found the answere, the bitflag operators are slower than the ==.
That means that writing something like this:
(0x01 & 0x10 == 0x11)
Ends up like:
(0x01 & (0x10 == 0x11))

Resulting in:
(0x01 & (false))


I never used bitflags in C, but I guess in C the bitflag operators are faster, thus making this a blizzard fail (just a guess, enlighten me if i'm wrong).

So we need the right bracketing here.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top