1. We've introduced thread tags, search within a thread and similar thread search. Read more here.
    Dismiss Notice
  2. We've released our first Timeline Experimental Preview, our new tool for creating cutscenes and more! To check it out click here.
    Dismiss Notice
  3. All Unity Pro perpetual license customers: your special offer to subscribe is ready! Click here.
    Dismiss Notice
  4. Unity 5.5 is now released.
    Dismiss Notice
  5. Check out all the fixes for 5.5 in patch releases 1 & 2.
    Dismiss Notice
  6. Unity 5.6 beta is now available for download.
    Dismiss Notice
  7. Get prepared for the Tizen Mobile App Incentive Program! Read more about the upcoming program here.
    Dismiss Notice
  8. Enter the Microsoft Developer Challenge for a chance to win prizes. Read more about it here.
    Dismiss Notice

Constants in Javascript?

Discussion in 'Scripting' started by dock, Mar 20, 2009.

  1. dock

    dock

    Joined:
    Jan 2, 2008
    Messages:
    376
    Code (csharp):
    1. static var RESET_TIME   = 2.0;
    I am presently defining constants like this. They are in a script named Constant.js, and I call them from other scripts like this:

    Code (csharp):
    1. if (timer >= Constant.RESET_TIME)
    However I'm aware this isn't the most efficient way to do it. Is there any way of making proper constants in Javascript that can't be adjusted at runtime?
     
  2. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Messages:
    2,061
    There's nothing wrong with what you're doing. In fact that's a proper and functional use of the static keyword.
     
  3. shawn

    shawn

    Unity Technologies

    Joined:
    Aug 4, 2007
    Messages:
    525
    Like Quietus said, computational wise there's nothing inefficient about what you're doing there. Though I would make the Constants class a singleton and make all of the "constant" variables into members of that singleton so that you can edit the values of them in the inspector. (Wish static variables were exposed in the inspector :( )

    Code (csharp):
    1.  
    2. var RESET_TIME:float = 2.0;
    3. var SOME_OTHER_CONSTANT:float = 5.0;
    4.  
    5. private static var instance:Constants;
    6.  
    7. function Awake() {
    8.  // singleton logic
    9.  if(instance) {
    10.   Destroy(this);
    11.   return;
    12.  }
    13.  instance = this;
    14. }
    15.  
    Then you can access the constants by with Constants.instance.RESET_TIME
     
  4. thecreatrix

    thecreatrix

    Joined:
    Dec 19, 2008
    Messages:
    94
    I am used to ActionScript, so maybe this doesn't apply to UnityScript, but is there a "const" keyword? In ActionScript, I would do it this way:

    Code (csharp):
    1. static const RESET_TIME:Number = 2.0
    (Sorry if I've been useless or confusing in this discussion!)
     
  5. jcarpay

    jcarpay

    Joined:
    Aug 15, 2008
    Messages:
    494
    Other than inspector exposure, is there an obvious reason to prefer singletons over static vars?
     
  6. Redbeef

    Redbeef

    Joined:
    Apr 9, 2013
    Messages:
    2
    Hey dude, just jumping over from AS as well, turns out Unity JS doesn't recognise 'const'.
    G