- Home /
Variable Accessors in UnityScript (Get{} and Set{})
I am (finally) learning the nuances of the Singleton design pattern, and was wondering if there's any way to use a variable accessor (get{} or set{}) in UnityScript. The c# syntax is quite simple and can be found all over the internet, but no matter what I try, I can't get it to work in JavaScript.
Here's some (pseudo)code to turn a class called WhirldIn into a singleton hosted by a GameObject to enable certain methods it contains to yield. It would work fine if I used a static var called S to store the singleton reference and a static function to initialize the var, but it just seems so much cooler to call WhirldIn.S.Load and have the singleton initialize itself if necessary :)
private static var s : WhirldIn = null; //Static Singleton Reference
static function get S () : WhirldIn { //Could be replaces with an init function
if (s == null) {
Debug.Log("Initializing WhirldIn GameObject");
var go : GameObject = new GameObject();
s = go.AddComponent(WhirldIn);
go.name = "_WhirldIn";
DontDestroyOnLoad(go); //Ensure the GameObject host of this Singleton class survives scene changes
}
return s;
}
Also, is there any practical overhead in always calling a .S accessor rather than just using a .S static var after initializing the singleton manually?
Here's some relevant threads I have been able to find:
http://answers.unity3d.com/questions/140946/c-enums-get-and-set-accessor-expected.html (Talks about a "get and set accessor in UnityScript")
http://www.unifycommunity.com/wiki/index.php?title=Singleton (C# Singletons)
http://forum.unity3d.com/threads/59621-Static-getter-setter (Discussion on how to make this work - but the solution gives me compilation syntax errors)
Answer by Bunny83 · Dec 18, 2011 at 12:08 AM
From what i've heared it's not possible to create static properties in UnityScript.
Read "Mike 3"'s answer on this question:
Edit : Fixed the link
Hmm yes - his answer does seem to address my situation directly. Strangely enough though, when I copy his code into my script: var moo = 1; function get $$anonymous$$oo() : int { return moo; } function set $$anonymous$$oo(value : int) { moo = value; } I get this error on the middle (get) line: "expecting EOF, found 'function'.". Not sure what's up with that...
@Aubrey Falconer: you need all of the code, not just the code inside the class declaration. Note what $$anonymous$$ike 3 said about "explicitly declared classes".
Heh, interesting. After I got his example to compile with your tip, the warning about static properties turned out to be completely correct. I sure wish Unity would work harder to discourage anyone from learning UnityScript until it achieves feature parity with C#... Was already planning to convert all my projects to C#, here's one more good reason to do so!
I don't think there's any reason to discourage anybody from using UnityScript. It's a wonderfully simple language that has never let me down once. That's nice that you like C#, but I have never needed it at all.
It would be great if you could help me understand the advantage of using properties. I've asked why people use them a couple times and have never gotten an answer. I also don't understand why there would be a need for static properties or what is wrong with using a static function in your example.
I have been a VERY happy UnityScript user for the past three years - I have written thousands and thousands of lines of code in it. Now that I am beco$$anonymous$$g a better programmer however, I wish that someone had made me start out in C# to begin with :) There are of course coders far more talented then myself who swear by UnityScript, but I am just giving my personal experiences here. When you start trying to do things like initialize a multidimensional array (for working with raw terrain data, for example) UnityScript just won't cut it anymore, and you will begin to get the urge to transition to the more powerful C#.
The example given on this page is honestly the only good use I have ever encountered for properties, and it has to be static in order to be useful:
I have a library which loads game worlds off of the internet, and provides resources for those worlds once they are loaded. It works perfectly fine for me to manually initialize the library (having it create a static reference for itself once initialized) when my application starts - but it would be far cooler for me to not worry about initializing the library, and ins$$anonymous$$d have it initialize itself automatically the instant it is needed.