- Home /
GUILayout items draw regardless of "IF" statements
Within my UI class I have the following enum set:
> public enum MessageBoxFlags
> {
> //Special
> None = 0x0,
> //Standard MsgBox templates
> Diplomacy = 0x1,
> ForeignNews = 0x2,
> HomeNews = 0x4,
>
> //Flag options for ForeignNews MsgBox
> ForeignDefeat = 0x8,
> ForeignVictory = 0x16,
> ForeignDeclaredWar = 0x32,
> ForeignSurrendered = 0x64,
>
> //Flag options for HomeNews MsgBox
>
> }
>
> public MessageBoxFlags msgTest;
This is used in OnGUI() to fire various windows depending on the enum value. One sets the window type and content through bitwise operations. Here I'm declaring the GUILayout.Window and its associated function:
> if((msgTest & MessageBoxFlags.ForeignNews) != 0) {
> Time.timeScale = 0.001f;
> GUILayout.Window(0,windowRect,
> news_fBox,
> "Our Foreign Minister Reports:");
> }
The problem is within the function "news_fBox". It simply acts as if all the IF statements are true and dumps one big window with everything in it. Here's news_fBox:
void news_fBox(int windowID)
{
Debug.LogWarning("msgTest value is: "+msgTest.ToString());
if((msgTest & MessageBoxFlags.ForeignDefeat) != 0)
{
GUILayout.Label("Following intense fights, "+countryActedUpon+" has
capitulated to "+countryInstigator+".");
GUILayout.BeginHorizontal();
if(GUILayout.Button("At least we're still alive."))
{
Time.timeScale = 1.0f;
msgTest = 0x0;
}
GUILayout.EndHorizontal();
}
if((msgTest & MessageBoxFlags.ForeignVictory) != 0)
{
GUILayout.Label("The brave people of "+countryInstigator+" have
defeated vile "+countryActedUpon+".\n May
their treacherous schemes never plague our
nations.");
GUILayout.BeginHorizontal();
if(GUILayout.Button("Serves them well."))
{
Time.timeScale = 1.0f;
msgTest = 0x0;
}
GUILayout.EndHorizontal();
}
if((msgTest & MessageBoxFlags.ForeignDeclaredWar) != 0)
{
GUILayout.Label(countryInstigator+" has declared war on
"+countryActedUpon+".");
GUILayout.BeginHorizontal();
if(GUILayout.Button("Ok"))
{
Time.timeScale = 1.0f;
msgTest = 0x0;
}
GUILayout.EndHorizontal();
}
if((msgTest & MessageBoxFlags.ForeignSurrendered) != 0)
{
GUILayout.Label("With morale shattered, having enough of all
the blood shed"+countryActedUpon+" has decided
to surrender to the "+countryInstigator);
GUILayout.BeginHorizontal();
if(GUILayout.Button("Fate help them all."))
{
Time.timeScale = 1.0f;
msgTest = 0x0;
}
GUILayout.EndHorizontal();
}
}
What happens is that, when sending the following value "Announcements.MessageBoxFlags.ForeignNews|Announcements.MessageBoxFlags.ForeignDeclaredWar" to msgTest, everything within news_fBox is dumped on the screen, regardless of the IF statement checks.
Also, when sending msgText.ToString() to the log, I only get "ForeignDeclaredWar" without "ForeignNews".
Am I using the GUI methods wrong or is there something wrong with my enum? Hm... for some reason I can't add anything related to GUI/UI as a tag because I lack the neccessary reputation. You'd think there's more than one person with a UI problem out there.
Answer by rutter · Mar 04, 2014 at 03:04 AM
Your bit flags don't appear to be set up correctly.
For example:
ForeignVictory = 0x16,
This gives 00010110
, which matches several of the bitmasks you provided.
If you're trying to mask individual bits, the left shift operator is usually simpler:
1 << 0 //(1st bit)
1 << 1 //(2nd bit)
1 << 2 //(3rd bit)
1 << 3 //(4th bit)
1 << 4 //(5th bit)
Oh, damn I'm stupid. Hex numbers go from 0-9 and A-F, no wonder I failed miserably. Your solution is much more elegant though so I'll mark it as an answer :)
Your answer
Follow this Question
Related Questions
If Check for serialized flag enums in Editor scripting 1 Answer
Use Enum of another script in an if statement C# 2 Answers
Editorscript: Generate enum from string[] 4 Answers
Optimising if else statements to switch and enums, or are there better methods? 1 Answer
Using an enum inside of a scriptable Object - why don't I see it in the inspector? 1 Answer