- Home /
Accessing enumeration values by their index in JS?
Hi everyone,
I'm wondering if there is a way to access an enumeration value by it's index? I'm trying to iteratively populate a hashtable where the keys are enumeration values and the hashtable values are prefab objects. Something like this:
enum VehicleType {car, boat, airplane, motorcycle};
var vehiclePrefabs : Transform[]; private var vehicleModels : Hashtable;
function Start () { vehicleModels = new Hashtable(); for (i = 0; i < vehiclePrefabs.length; i++) { vehicleModels.Add(VehicleType[i], vehiclePrefabs[i]); } }
This is so ultimately I can do this:
function SpawnVehicle(vehicleType : VehicleType, spawnPoint : Vector3)
{
var spawnVehicle = Instantiate(vehicleModels[vehicleType], spawnPoint, Quaternion.identity);
}
Now when I try to access the value by saying VehicleType[i] I get an error about "Type 'System.Type' does not support slicing."
I searched the forum a bit and found a post that mentions using Enum.Getnames(VehicleType), but that seems to get all the names at once, and I want them one at a time. And I can't seem to find anywhere in the Scripting Reference that adequately describes what you can do with Enum..
Any answers or clarification in what I'm doing wrong and what it's possible to do in terms of manipulating enumerations would be appreciated!
-Dylan
Answer by Eric5h5 · Nov 01, 2010 at 12:31 AM
Enums are described in the .net docs; they're not Unity-specific so they won't be documented in the Unity manual. However, I don't see a need for a hashtable here. It will work fine like this:
enum VehicleType {car, boat, airplane, motorcycle};
var vehiclePrefabs : Transform[];
function SpawnVehicle(vehicleType : VehicleType, spawnPoint : Vector3) { var spawnVehicle = Instantiate(vehiclePrefabs[vehicleType], spawnPoint, Quaternion.identity); }
Then you can do
SpawnVehicle(VehicleType.boat, Vector3.zero);
or whatever. Enums are just ints which are referred to by name, not strings. Though you can substitute the actual value if you want. i.e.,
SpawnVehicle(1, Vector3.zero);
Note that you can explicitly define the values:
enum VehicleType {car = 0, boat = 1, airplane = 2, motorcycle = 3};
You can use whatever numbers you want (as long as they are positive integers); they don't have to be in order.
Ah, gotcha. I thought I had to go through an extra step to associate the enum names with the prefabs I want them to refer to. But I totally see how this works. $$anonymous$$uch easier. Thanks!
So you can't use a negative number in an enumeration in Unityscript? I've seen it done in C#, so it seemed odd to me. Both are capable of the same in $$anonymous$$OST cases.
I'm having trouble understanding this . I have written (uJS) :
for ( var i : int = 0; i < System.Enum.GetValues( AttributeName ).Length; i ++ )
{
var enumValue : AttributeName = i;
GUI.Label( Rect( 10, i * 40, 100, 25 ), enumValue.ToString );
}
but this throws the error : BCE0023: No appropriate version of 'UnityEngine.GUI.Label' for the argument list '(UnityEngine.Rect, function(): String)' was found.
it seems for C# this can be done with :
GUI.Label( Rect( 10, i * 40, 100, 25 ), ((AttributeName)i).ToString );
where am I going wrong? Thanks.
I think
var enumValue : AttributeName = i;
should be
var enumValue : AttributeName = (AttributeName)i;
What you are trying to do, assigning an int to an enum, works with C but was dropped in C++, C#.
I would think that is one of the drawback of UnityScript, C# would tell you wrong and you would not pass compilation.
Thanks, unfortunately this throws the error : UCE0001: ';' expected. Insert a semicolon at the end.
What I'm trying to achieve is to iterate through all the values of the enum, and display them (as a string), eg :
public enum AttributeName
{
$$anonymous$$ight,
Constitution,
Nimbleness,
Speed,
Concentration,
Willpower,
Charisma
}
I assumed enums worked both ways, so if I specified the index of the enum, it would return the value, eg 0 returns $$anonymous$$ight, 3 returns Speed, etc. Just as parseInt( AttributeName.$$anonymous$$ight ) works the other way.
This is what I found before writing the above code : http://answers.unity3d.com/questions/42743/integer-value-casting-to-keycode-enum-in-js.html
Answer by StephanK · Nov 01, 2010 at 12:32 AM
An enum type can be implicitly cast to an integer. So you can access your vehicleModel array by writing vehicleModels[VehicleType.car];
Your answer
Follow this Question
Related Questions
Insert a string to a list inside a list 3 Answers
Making a user-friendly array with indices based on an enum 2 Answers
Javascript array: Negative Indexes? 2 Answers
[CLOSED] UnityScript - enum to numeric value error 1 Answer
Javascript Enum Question 1 Answer