Trying to figure out the best way to do bitwise operations in UnityScript, it seems to work, but the problem I have is that I need to convert my enum to int for any operation.
Is it possible to explicitly declare the enum as an int when you create it?
Here is stuff that works, but can I get around using parseInt() all of the time?
enum myEnum
{
None = 0,
First = 1,
Second = 2,
Third = 4
}
var someValue : int = myEnum.First | myEnum.Third;
var isFirst : boolean = (someValue | parseInt(myEnum.First)) == someValue;
var isSecond : boolean = (someValue | parseInt(myEnum.Second)) == someValue;
var isThird : boolean = (someValue | parseInt(myEnum.Third)) == someValue;
So if I want to do bitwise operations, I always need to cast the enum as int or it gives me an error
someValue = someValue | parseInt(myEnum.Second);
Lastly, is there any way to get this syntax to work in UnityScript? Gives an error that it doesn’t expect the operators
enum myEnum
{
None = 0,
First = 1,
Second = 1 << 1,
Third = 1 << 2
}
On a side note, what are the advantages in doing this bitwise stuff instead of just using some boolean values to begin with? Because the later would actually simplify quite a few things, especially with extending the editor GUI.
Enums aren’t ints; if you want ints then you should just use ints. You haven’t said what you’re trying to do, so I can’t tell you what’s the most appropriate. If you need to do bitwise operations, certainly booleans won’t help you at all since they’re completely different.
Yeah, I really just want to learn how to do it in UnityScript if possible, even if I don’t use it.
What I did in the first post actually works with bitwise operations, but it seems like there should be a more elegant way.
I have a php/js background so UnityScript is very easy to pickup. I can do C++ etc, but still have lots to learn there, so would take a fair bit more time.
If I have to I will learn C#, but I haven’t seen a need yet.
enum MyColor
{
Yellow = 1,
Green = 2,
Red = 4,
Blue = 8
}
var allowedColors : int = MyColor.Red | MyColor.Green | MyColor.Blue;
if(allowedColors MyColor.Red)
{
// Red has been set...
}
in c# you (annoyingly) have to cast an enum to an int… not sure if that is the case here, but try:
if(allowedColors (int)MyColor.Red == (int) MyColor.Red)
Since you’re already using an enum, you don’t need to type “allowedColors” as an int. You should continue to use it as an enum type (rather, MyColor type) until you need to convert it to an int (say, for storage/display purposes)
var allowedColors : MyColor = MyColor.Red | MyColor.Green | MyColor.Blue;
if ((allowedColors MyColor.Red) == MyColor.Red)
{
}
Note the usage of the brackets in the IF statement (which I forgot in my first post) is required.
Yeah that’s why I was wondering if it was possible to cast a Javascript enum to an int. I’m not sure if you can do (int) in brackets to cast in javascript, but I guess parseInt is the equivalent there.
Hmm I’ll try it again, but I originally tried that and it just wasn’t working. It would allow it and not give any errors, but the If statement wouldn’t result in true.
The reason you have to cast is because in C#, the enums aren’t just a number. They’re a number associated with a type. If they had implicit operators to integers, then you’d be able to directly compare (and possibly assign) mixed enumerations. Consider the following:
enum Foo
{
First = 1,
Second = 2,
Third = 3
}
enum Bar
{
First = 1,
Second = 2,
Third = 3
}
Foo foo = Foo.Second;
Bar bar = Boo.Second;
//the following make no sense, and are dangerous
if (foo == bar)
if (foo == Bar.Second)
if (Foo.First == Bar.First)
foo = Bar.Third;
Ugh… nevermind. I swear I tried all of this yesterday, for some reason it didn’t work. I must have been doing something wrong.
This is all working now, even the shorter version of what you did
// This works
if ((allowedColors MyColor.Red))
{
Debug.Log("PASS");
}
else
{
Debug.Log("FAIL");
}
// This works
if ((allowedColors (MyColor.Red | MyColor.Green) ))
{
Debug.Log("PASS");
}
else
{
Debug.Log("FAIL");
}