x


How to declare and use a enum variable in Javascript?

What is the correct way to declare and use a enum variable, in Javascript?

more ▼

asked Apr 16, 2010 at 10:51 AM

Mattivc gravatar image

Mattivc
2k 136 113 124

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?

Apr 13, 2011 at 01:49 PM FalconRime

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.

Apr 13, 2011 at 02:05 PM Toxic Blob

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?

Jan 06, 2012 at 09:50 PM spacepilot

Positive integers, actually... it's one of the limitations of enums.

May 04, 2012 at 08:49 PM aviose

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.

May 04, 2012 at 09:19 PM Bunny83
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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...
    }
}
more ▼

answered Apr 16, 2010 at 11:27 AM

duck gravatar image

duck ♦♦
46.8k 127 188 453

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.

Jun 15, 2010 at 08:55 AM Marowi

thanks for the pointer! just edited it to fix that.

Jun 15, 2010 at 10:10 PM duck ♦♦

Is it true that enum declarations have to be outside of a function?

May 18, 2011 at 11:51 PM RoxPhilosopher
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Apr 13, 2011 at 02:03 PM

Toxic Blob gravatar image

Toxic Blob
701 48 38 55

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?

Oct 12, 2011 at 11:00 PM sabliao

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)

Mar 30 at 06:52 AM andyisbonza
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1338
x220
x128
x94
x10

asked: Apr 16, 2010 at 10:51 AM

Seen: 63574 times

Last Updated: Mar 31 at 06:33 AM