Convert Enum to String so I can use "IndexOf"

Started by Knox, Mon 27/09/2010 23:46:20

Previous topic - Next topic

Knox

Hi,

Im pretty sure this will be answered fairly easily, but how do I convert an enum to a string so I can use "IndexOf" on it?
I tried to simply place a return inside a string variable, but this doesnt work:

 
Code: ags
 
  eFaceAnim currentFa = this.GetFaceAnim();
  Character* cChar = this;
  
  //if faceAnim is already the same as the change... dont save the backup
  //is the word eSad contained inside the string currentFa?
  
  String sCurrFa = return currentFa;
  int iResult = sCurrFa.IndexOf("Sad");
  Display("%d", iResult);
  
--All that is necessary for evil to triumph is for good men to do nothing.

Khris

You can't convert variable names into variable contents or the other way 'round. Programming doesn't work that way.

The easiest way is to do

Code: ags
  if (currentFa == eSadBlah || currentFa ==eSadBleh || currentFa == eSadBluh) ...


Another way is to assign int values to the enum names:
Code: ags
enum blah {
  eOne = 1,
  eTwo = 2,
  ...
};


That way you can e.g. use 10 to 19 for all those containing Sad, then check if (currentFa/10 == 1).

Knox

#2
Ah crap I thought so...I believe I had a simliar question but I was hoping this was different!

I currently have done your first suggestion but was hoping to save space with indexOf...Ive got quite a bit of varieties so it will be quite long!
Ill see if your 2nd suggestion gives me less work :P
thanks :)

QuoteThat way you can e.g. use 10 to 19 for all those containing Sad, then check if (currentFa/10 == 1).
ok so (for example) 19/10 will get truncated to an int (not float), so it will return 1, right? Interesting...
--All that is necessary for evil to triumph is for good men to do nothing.

Khris

Yes, as long as currentFa is an int, the result is also an int (if it were a float, you'd have to also divide by a float).
Decimal places are disregarded with ints.

GarageGothic

Instead of using enums (which I personally find annoying to work with because adding/changing one in a module header requires a full recompile), you could set up either parallel arrays of Strings and ints (combined in a struct or as two separate arrays), or simply an array of Strings that hold both the "name" and the int/float value separated by a special char. I used this approach for my own "emoticons" within dialog that are then parsed to change the character faces and of course removed before displaying the dialog String. Works great.


monkey0506

Quote from: Khris on Tue 28/09/2010 01:32:11You can't convert variable names into variable contents or the other way 'round. Programming doesn't work that way.

Just some semantical belligerence here, but that really depends on the language in question. Case in point, in PHP it's possible to store the string-literal "hello" into a variable (say, $a), and then without knowing the contents of that variable, create a variable with the same name as the contents (namely, $hello) (See Variable variables).

Obviously in the context of AGS programming what you said is correct..but since knox is taking a crack at learning to program I think it's best to keep an open mind toward what is possible for a computer to do, despite limitations of the given language you are working with. ::)

Khris

I'm aware of this and it seems to be possible in Action Script, too. The thing is that thinking along these lines has lead general_knox to approaching coding problems from the wrong direction multiple times already.
And since it isn't possible in all of the well-known higher languages, removing it from the list of ways to address a problem should be in order.

Knox

#7
Well the main reason why I tend to "think" the way I do about variables, strings, etc...is due to the fact I use maya's mel script often, and its permitted to use variables + strings in that way...its kinda hard to unlearn since its so automatic for me!

...slowly but surely it will get pounded into my brain!!

Some of Mel script string functions:  ;D

Holy crap it would be great to have some of these in AGS (the ones we dont already have, that is)...especially "tokenize"!!
--All that is necessary for evil to triumph is for good men to do nothing.

Knox

#8
Ok, I was just playing around with stuff and I was wondering what you guys thought of it...is it too complicateed for nothing? I kind of tried to do my "convert enum to string" manually...

*ps: I left out other emotions so its not so long in this post

Code: ags


void SetFa(this Character*, eFaceAnim eFa)
{
  this.SaveSettings_ToVariables("Backup", eiF);
  this.SetFaceAnim(eFa);  
}

void setStringTo(String sThisString)
{
  sAsString = sThisString;
}

void SetUpFaceAnims_AsStrings()
{
  //setup converting manually character enum to strings
  //BRIAN
  if (currentFa == eFa_BrianYng_eNeutral) setStringTo("Yng_eNeu");
  else if (currentFa == eFa_BrianYng_eMad) setStringTo("Yng_eMad");
  else if (currentFa == eFa_BrianYng_eSad) setStringTo("Yng_eSad");
  else if (currentFa == eFa_BrianYng_eHappy) setStringTo("Yng_eHap");
  else if (currentFa == eFa_BrianRSh_eNeutral) setStringTo("RSh_eNeu");
  else if (currentFa == eFa_BrianRSh_eMad) setStringTo("RSh_eMad");
  else if (currentFa == eFa_BrianRSh_eSad) setStringTo("RSh_eSad");
  else if (currentFa == eFa_BrianRSh_eHappy) setStringTo("RSh_eHap"); 
  //else if...add more versions of Brian here...
  
}

function ChangeEmotion(this Character*, eEmotions eEmotionType)
{
  currentFa = this.GetFaceAnim();
  SetUpFaceAnims_AsStrings();
  if (sAsString != null)
  {
    if (eEmotionType == eSad)
    { 
      //Display("Set Emotion: Sad");
      if (!(sAsString.EndsWith("_eSad", true))) // NOT one of the sad anims
      {
        //BRIAN
        if (sAsString.StartsWith("Yng_", true)) this.SetFa(eFa_BrianYng_eSad);
        else if (sAsString.StartsWith("RSh_", true)) this.SetFa(eFa_BrianRSh_eSad);
        else if (sAsString.StartsWith("RSt_", true)) this.SetFa(eFa_BrianRSt_eSad);
        else if (sAsString.StartsWith("Mid_", true)) this.SetFa(eFa_BrianMid_eSad);
        else if (sAsString.StartsWith("Old_", true)) this.SetFa(eFa_BrianOld_eSad);
        
        //Other Characters here       
      }
      //else if...other emotions here
    } 
}


Im not sure if its more complicated to do this way or just do it the way Khris suggested (1st suggestion).

Ill check out parallel arrays too (wikipedia for sure), GarageGothic...never used that before!

Also...is there a possibility of creating a plugin or expanding your StringPLus module, Monkey...so AGS can "see" variable/enum returns as a string that can be used with string functions? That way I wouldnt need to convert those enums manually to strings...
--All that is necessary for evil to triumph is for good men to do nothing.

Wyz

I looks like you're trying to see if a values belongs to a range of enumerated values. In this case you could use a bit more efficient method:
Code: ags


enum Food
{
  eFruits, 
  eApple,
  eBanana, 
  eOrange,
  
  eBeverages, 
  eCoffee, 
  eTea, 
  eMilk, 
  
  eVegitables, 
  eCabbage, 
  eCarrot, 
  eTurnip 
};



in this way you could find out if something is a fruit like this:
Code: ags

if ((value > eFruits) && (value < eBeverages))
  ...


Or if something is not a beverage:
Code: ags

if ((value < eBeverages) || (value > eVegitables))
  ...
Life is like an adventure without the pixel hunts.

Knox

Ok well thats almost like Khris's 2nd suggestion (grouping all emotions together by type)...so Id have to group my character's face anims by emotion instead of by char name, which is not a big deal....ok, Ill try that!

Too bad there wasnt a function "convertReturnToString" or something :P

**Im checkin the StringPlus module now to see if I can get more options!
--All that is necessary for evil to triumph is for good men to do nothing.

SMF spam blocked by CleanTalk