- Home /
How to declare and use a enum variable in Javascript?
What is the correct way to declare and use a enum variable, in Javascript?
Can you comment on whether (1) the enum declaration and (2) the use of the variable, has to be in the (a) the same script, (b) a script on the same object or (c) doesn't matter?
To keep things organised I have all my enums in their own scripts in a separate folder (Scripts/Enums). If your enums are within a class you'll have to specify the class prior to the enum to access it, which is often needlessly awkward.
Is enum limited to integer-values? This site here states that the sub-vars of an enum-declaration are always integer. I need float - is there a way?
Positive integers, actually... it's one of the limitations of enums.
Exactly. If you use the enum as a variable type, the variable is actually just an int32 behind the scenes. This is a fix behaviour and can't be changed.
You can use an enum + an array with float values and use the enum constants as index into the array to get the float value.
Answer by duck · Apr 16, 2010 at 11:27 AM
Like this (using the example of various AI states for an AI State Machine):
enum AIState { Asleep, Idling, Chasing, Fleeing, HavingLunch }
And then you can assign any value from that enum to a variable, like this:
var state = AIState.Idling;
In the above example, the variable's type is implicitly defined as "AIState". You can explicitly define a variable with that type like this:
var state : AIState;
And similarly you can define the input parameter of a function to recieve a value from your enum, like this:
function PerformAction( currentState : AIState ) {
switch (currentState) {
case AIState.Asleep:
Debug.Log("Zzzz.");
break;
// etc...
}
}
I've upvoted your answer because it helped me, but your example code is slightly incorrect. In the enum declaration, you've named it 'Asleep', but in the switch, you're checking for 'Sleeping'. Cheers.
Is it true that enum declarations have to be outside of a function?
Answer by Toxic Blob · Apr 13, 2011 at 02:03 PM
You can also assign values to your individual enum elements.
enum Layer
{
Protagonists = 9,
Antagonists = 10,
HeadsUpDisplay = 11,
LevelBoundaries = 13
}
This is incredibly helpful if you use layers, for example. Rather than using
this.gameObject.layer = 9;
You could instead use the easier to read
this.gameObject.layer = Layer.Protagonists;
This is especially helpful as there is then only one location, your enum, where you have to change the layer number if a change is needed.
What would be the purpose of assigning them different layers? Is it one way to have another kind of differentiation? Such as only finding objects within a certain layer?
That is a great tip - upvoted!
I use layers to stop objects from colliding with each other when I don't want them interacting. If, for example, you had an enemy that was unaffected by bullets, one option would be to put them on a different layer from each other, then ensure that the bullet's layer and the enemy's layer didn't cause collision (under Project settings --> Physics)
Your answer
Follow this Question
Related Questions
EnumStates 1 Answer
Compare 2 enum variables method? 1 Answer
Field of view, using raycasting 5 Answers
Can you end a coroutine from outside of that coroutine? 2 Answers