- Home /
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!
Answer by Eric5h5 · Jan 16, 2012 at 01:17 PM
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.
There is no way to achieve the gain you discuss. Local variables typically perform slightly better than global variables. Also they make the code better, so always try to use local variables where possible.
$$anonymous$$agnificent - thank you for your expertise. Cheers for now.
Answer by MartinCA · Jan 16, 2012 at 02:36 PM
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
Answer by Statement · Jan 16, 2012 at 11:13 AM
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
}
Hi Statement! Thanks -- look, what difference could it make to add "static" to a variable declared out there in the file? (in a class?) I mean surely it is "static" anyway right ???? (How else could it be interpreted?)
PS if this is a true fact: "You can't make local variables static (those declared in a function)." then you've answered the question, thanks. But I still don't see what "static" could mean in a file out in the general area outside functions - it's all static there.
No, variable declared outside functions are member variables: they exist independently in every instance of the class. A static variable is unique, has program scope and program life time. It doesn't matter if you have instantiated several instances of the script where it's defined: the static variable is the same for all of them - and it exists even if you don't instantiate the script at all!
Answer by Owen-Reynolds · Jan 16, 2012 at 05:37 PM
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.
Your answer
Follow this Question
Related Questions
Static Variable Problem 1 Answer
Accessing another var on another script 1 Answer
variable = true from another script 2 Answers
Unknown identifier, OnCollisionEnter Error 1 Answer
Global variables - static keyword ? UnityScript? javascript? 1 Answer