Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: mode7 on Wed 06/04/2011 20:29:34

Title: Putting enum text value in a string
Post by: mode7 on Wed 06/04/2011 20:29:34
I'm trying to display an enum at a label. But i can only do so with the integer values.
Is there a possibility to show the enum text instead of the int value?
lstate.Text = String.Format("State: %d",CurrentState); //this i have now

lstate.Text = String.Format("State: %s",CurrentState); //this won't work
Title: Re: Putting enum text value in a string
Post by: monkey0506 on Wed 06/04/2011 21:07:48
Unfortunately there's no direct way in AGS to do this, but you could define a custom specialized function to do it for you:

String GetEnumName(EnumType value)
{
  if (value == eValue1) return "Value1";
  if (value == eValue2) return "Value2";
  if (value == eValue3) return "Value3";
}

// ...
lstate.Text = String.Format("State: %s", GetEnumName(CurrentState));
Title: Re: Putting enum text value in a string
Post by: Sephiroth on Wed 06/04/2011 21:13:26
*Edited:

Sorry if this is obvious but, why not use a simple string:


string CurrentState;

CurrentState = "Poisoned";
Label.Text = CurrentState;


Quote
show the enum text instead of the int value.

What you're referring to when you say 'text', is actually like a (int) variable.


enum Days {monday, tuesday, wednesday};

Days CurrentDay;
CurrentDay = monday;

//is the same as:

CurrentDay = 0;

Title: Re: Putting enum text value in a string
Post by: mode7 on Thu 07/04/2011 00:00:08
Ok thanks a lot guys!
Yeah I know its like an int, I just thought there might be an easy way to do this.
Well a specialized function is way too complicated and I want to use the enum for convinience reasons.
So I'll just have to note down the numbers instead.
Title: Re: Putting enum text value in a string
Post by: monkey0506 on Fri 08/04/2011 19:18:03
I don't see how "not[ing] down the numbers" is a simpler solution than the one I presented (or indeed why the one I suggested is "complicated" at all)..perhaps if you're only doing this for debugging purposes, that would make a bit more sense..but in any case, at least you got a solution for yourself. ;)
Title: Re: Putting enum text value in a string
Post by: mode7 on Fri 08/04/2011 20:09:22
I'm just doing this for debugging reasons, yes. So no reason to blow up the code any further. I'd also have to edit this function everytime i add a state - so this is not really practical.
-thanks anyway monkey.