Search Unity

Exposing variables in the inspector JS

Discussion in 'Scripting' started by pete, Jan 6, 2007.

  1. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    from the c# mouselook script...
    Code (csharp):
    1.  
    2.     public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
    3.     public RotationAxes axes = RotationAxes.MouseXAndY;
    4.  
    5.  
    this gives you a pull down in the inspector and initially sets it to MouseXandY. any pointers on doing this in javascript?
     
  2. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    i found an older post (last april) that says you have to use enums through c# but that javascript would be supported in the next major release. did that make 1.6?
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    If you used to need to use enums in C#, you don't now. I use JS enums a lot.

    Code (csharp):
    1.  
    2. enum Mode {Menu, Construction, Arena}
    3. var CurrentMode : Mode = Mode.Menu;
    4.  
    There are some limitations to this - for example, arrays of enums will not show up in the inspector like, say, an array of strings - but that's the jist of it.

    If you see an integer displayed in the inspector, first make sure you've explicitly declared the type of the variable ( : Mode, or whatever your enum type is called) and if that doesn't work, restarting Unity fixes everything ;-)
     
  4. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    i'm betting i didn't declare the type correctly. i'll try it again. thanks!
     
  5. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    works like a charm and indeed it didn't at first. i had it declared correctly but it was a relaunch that got it working. gotta remember that one. thanks again!
     
  6. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Actually you only have to deselect the object in the inspector and select it again. (Right now Unity doesn't auto update the inspector when a type changes from int to enum, since it internally is treated in the same way.)
     
  7. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    well hey, that's better than a relaunch... thanks for the info!
     
  8. Der Dude

    Der Dude

    Joined:
    Aug 7, 2006
    Posts:
    213
    A bit OT, but could someone explain the advantage of using Enums over Classes?

    I understand that it is handy for encapsulating data, however, this might as well be done by defining a new class. Is this a performance issue?
     
  9. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    Enums are used to give names to a set of constants. So, for example, defining enum WindowState {Open, Closed}; would allow you to have variables of type WindowState which can be assigned the values WindowState.Open or WindowState.Closed. That's generally preferable to having an int or string variable to which you assign arbitrary values, as it makes it harder to accidentally use a value which isn't valid.

    It has nothing to do with data encapsulation or efficiency. It's solely to do with putting human-readable names onto values, and making sure you can only use them as intended.
     
  10. Der Dude

    Der Dude

    Joined:
    Aug 7, 2006
    Posts:
    213
    Well I guess in your example it would be easier to use 1 boolean, especially as a window can't be closed and open at the same time, but I think I know what you mean.
     
  11. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    Sure, but it might be clearer to label it with something which explains what it does when it's used as a parameter to a function. For example, see SendMessageOptions, used as a parameter to SendMessage. If SendMessage took a bool as its last parameter, it'd be much more difficult to tell what it does at a glance.

    And if you have three or more values, it's obviously even more meaningful. :)
     
  12. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    when i couldn't get it working i had it as an int. in the inspector i had to enter 0, 1 or 2 depending on what i wanted my script to do. someone else would have to look at my script and hope i commented.

    instead with enums, i now have a pull down in the inspector and i can select MouseXandY, MouseX or MouseY.
     
  13. Der Dude

    Der Dude

    Joined:
    Aug 7, 2006
    Posts:
    213
    Oh, I forgot that you only store Constants there. Then my last post is total rubbish.

    Now I've gotcha. Thanks!