Trivial question for Javascript/Unityscript experts: static variables?

Attn Unityscript EXPERTS:

in normal computer languages, you might mark a variable “static” …

my happy high performance function ()
   {
   static my variable X
   do lots of processing, use X as much as you like
   }

so as to give a performance hint to a modern compiler that you do not want to go to the effort of instantiating the variable, or indeed resetting the variable, every time (or indeed … you may algorithmically need the value to be maintained between calls, but here i’m interested in the performance aspect).

As far as I can see the tag “static” does not exist in Unityscript. Normally for a similar effect I just do this:

private var _XitsMoreOrLessLikeAStatic : float;
function Happyfunction ()
   {
   do lots of processing, use _XitsMoreOrLessLikeAStatic as much as you like
   }

(in fact interestingly that can improve performance slightly - some tests seemed to show so, anyway.)

Obviously that works fine but is simply a little untidy, you have all those variables hanging out in the script at hand.

I have absolutely no understanding, at all, of Javascript, Unityscript, the compilation ? / interpretation ? paradigms at work in the stack all the way between unityscript and the game running on an iPad … so given that, can you tell me …

… is there a way to mark variables “static” in that sense? is it anyway (perhaps?) completely meaningless in the Unity milieu? have I just screwed-up something simple and “static” is spelled differently, or some such? Help!

Thanks!

Static is spelled “static”, but there is no point at all trying to use that for performance. The only reason you would use a static variable is if you have a global variable that should have one instance per class, rather than one instance per object.

I think you’re thinking in terms of native languages, and this is why you misunderstand the static behavior is Unity.

Unity uses C# and JavaScript, which are class-based languages and as such the program itself is a class, and every program component is also a class. As opposed to native languages where the execution entry point is a function, in C#/Java the execution entry point is a class.

That said, the definition of a static variable in such context, is a variable that exists OUTSIDE of a class instance, and exists only ONCE for every class definition. Consider this pseudo code:

SomeClass
    public SomeVariable

OtherClass
    SomeClass cls
    SomeFunction()
         cls.SomeVariable <---- ACCESSING AN INSTANCE OF SomeClass

In this case, using public variables, I would have to instantiate the class to create an instance of SomeVariable to access it.

Static variables and functions are exposed outside of the scope of the instantiated class, and only one such instance exists globally:

SomeClass
    public static SomeVariable

OtherClass
    SomeFunction()
         SomeClass.SomeVariable <----- ACCESSING A STATIC INSTANCE

As you can see, you do not need to create an instance of SomeClass to access it’s static members.

As for optimizations, using the static keyword when appropriate surely boosts performance, however the considerations for optimizing high-level managed program are different from optimizing native programs.

You can read more on C# static keyword here

Just add “static” before the declaration. You can’t make local variables static (those declared in a function).

static private var staticFloat : float;
function Happyfunction ()
{
    do lots of processing, use staticFloat as much as you like
}

The confusion is that the static keyword has two completely different meanings, depending whether you use it in a function (which C# can’t do) or a class.

The earliest, in C++ was what you are asking about – it makes a local variable be persistant. Usually things like (not a great example): bool clickCounter() { static int clicks=0; clicks++; return (clicks>10); } Before static, we’d have to make clicks be a global, with a comment about how only clickCounter should use it.

Nowadays, we’d make a class called ClickCounter, member variable clicks and member functions Add and isDone. That would automatically make clicks be persistant and local.

A useful example of the other static (the one C#/UnityScript has) in a class:

class String {
  static const int MAX_SIZE = 999; // all strings share this
  int size; // each string has its own size
   ...
}

This tells readers that there is one MAX_SIZE for all strings – you don’t need to set it for each one. It also saves space, since each instance of a string doesn’t need to store MAX_SIZE.