- Home /
Problem with MaskField ints
I am trying to script a MaskField with a dynamic list of Object names, then add/remove Object from another list depending on the int value derived from the MaskField, but maskfield seems to always give out a full "11111111111111111111111111111111" when "everything" is checked and it prints an int of -1, regardless of the amount of objects in the list. So if i have a list of two things, it will give me that for "everything" and if i uncheck 1 thing it will give me a number like "11111111111111111111111111111101" and prints as -3;
so to summarize:
MaskField has 2 listed objects. Select one and i get binary of "1" Select the other and i get binary of "10" Select "Everything" and i get binary of "11111111111111111111111111111111", int prints as -1. Deselect one and i get binary of "11111111111111111111111111111101", int prints as -3.
i tried using unsigned ints and longs and none of that works
Answer by Bunny83 · May 07, 2015 at 09:16 AM
Well, and what's the problem with that? It's a bit mask so it behaves exactly the way it should be. If you only need the last two bits just use a bitwise "and" ( & ) with 3 and you only get the last two bits. Or to test a single bit in the mask use
if((maskValue & (1 << bitIndex)) > 0)
where "bitIndex" is an index starting at 0.
Since it's a bitmask you shouldn't treat the int value as a whole. You usually test if certain bits are set or not.
If you want to mask only the lower "n" bits you can use this:
int lowerBits = maskValue & ((1 << bitCount)-1);
where bitCount is the count of bits you want, in your case 2.
Your answer
Follow this Question
Related Questions
How to assign a field within a PropertyDrawer? 1 Answer
exclude public field from inspector 2 Answers
How to record hideFlags for Undo/Redo 0 Answers
Exposing Enums in Custom Inspector 1 Answer