- Home /
Enum ToString via Int (JS)
for (var L in lines)
{
var Done1 : boolean = false;
var S = L.Substring(5).Split(";"[0]);
ImportedItems.Add(ImportedItems[0]);
var values = S[0].Replace(" ","").Split(", "[0]);
ImportedItems[x].Name = values[0];
ImportedItems[x].Desc = values[1];
ImportedItems[x].Price = float.Parse(values[2]);
Debug.Log("String version of Type: " + values[3]);
/*if(values[3] == "Loot")
{
ImportedItems[x].Type = 1;
}*/
for(var x = 0; x < 10 && !Done1; x++)
{
if(float.Parse(values[3]) == Type[ x ])
{
ImportedItems[x].Type = float.Parse(values[3]);
Done1 = true;
}
}
if(x == 10 && !Done1)
Debug.LogError("Error: Item Type fail!");
I have this piece of code and need to know how to fix the if statement... "Type.x.ToString("F")) obiously doesn't work. So how do i take the x value of the enum, and make it a string? Anyone? Inside "Type" there is:
enum Type { Loot = 1, Food = 2, Weapon = 3, Hat = 4, Shoulders = 5, Gloves = 6,
Body = 7, Pants = 8, Boots = 9, Coins = 10, none }
Inside the text asset value is taken out of is the following:
Item: Sword, An old rusty Sword, 15, Weapon;
Item: Axe, An old rusty Axe, 10, Weapon;
Item: Toast, Tasty apple pie, 500, Food;
Item: Battery, Metallica and metal, 5, Loot;
The number values in the enum was a way to possibly fix it by putting a float inside the textasset but i still needed to acesss Type with 'x'.
Thanks in advance.
-CDK
By the way the following error comes with the line when i do "Type[x]": (43,43): BCE0048: Type 'System.Type' does not support slicing.
Using an enum called Type
is going to get confusing for the compiler. Call it $$anonymous$$yType
or something other than Type
.
Answer by Graham-Dunnett · May 08, 2013 at 07:42 AM
I believe your question is:
"How can I convert a string to an enum of the same name?
enum MyType { Loot = 1, Food = 2, Weapon = 3, Hat = 4, Shoulders = 5, Gloves = 6,
Body = 7, Pants = 8, Boots = 9, Coins = 10, none };
MyType fred = ConvertFunction("weapon");
How should I write ConvertFunction
?"
In which case just have a big if-else if
statement that maps the strings to the enum values.
There have to be a better way than ALOT of if(values[3] == "Loot") { ImportedItems[x].Type = 1; } EDIT: If there isn't i guess i have to do it.. But it just doesn't sound right.
Answer by DaveA · May 08, 2013 at 07:51 AM
See http://msdn.microsoft.com/en-us/library/system.enum.tostring.aspx and http://msdn.microsoft.com/en-us/library/system.enum.getvalues.aspx and http://msdn.microsoft.com/en-us/library/system.enum.tryparse.aspx
Not to be an ass but that's all answers if i was using C#... Which im not using.. Which i said multiple places ;)