UnityScript Getter Setter : Are they supposed to work?

Discussion in 'Scripting' started by GibTreaty, Aug 6, 2011.

  1. GibTreaty

    GibTreaty

    Member

    Joined:
    Aug 25, 2010
    Messages:
    583
    I'm trying to convert a C# script to UnityScript but I'm having problems converting the getter setter. I've tried searching for a way to do it but couldn't come up with an answer. Am I doing it right or do they simply not work at all in UnityScript?

    C#

    Code (csharp):
    1.  
    2.     float _chargeTime = 1;
    3.  
    4.     public float chargeTime {
    5.  
    6.         get { return _chargeTime; }
    7.  
    8.         set { _chargeTime = Mathf.Clamp(value, 0, 1); }
    9.  
    10.     }
    11.  

    UnityScript

    Code (csharp):
    1.  
    2.    private var _chargeTime = 1.0;
    3.     var chargeTime : float {
    4.         get { return _chargeTime; }
    5.         set { _chargeTime = Mathf.Clamp(value, 0, 1); }
    6.     }
    7.  
  2. zine92

    zine92

    New Member

    Joined:
    Nov 13, 2010
    Messages:
    1,347
    Nope. The Get and Set is special method in C# which is just not supported in javascript and certainly not Unity javascript.
  3. Dreamora

    Dreamora

    Member

    Joined:
    Apr 5, 2008
    Messages:
    26,586
  4. Wennid

    Wennid

    New Member

    Joined:
    Jul 30, 2011
    Messages:
    9
    Properties are just another thing Micro$oft ripped off from C++ (and others, and then messed up, as usual). You can achieve identical functionality (but admittedly less automated error trapping/prevention) the old-fashioned "long-hand" way by creating getter and setter functions

    function get_chargeTime() { return _chargeTime; }
    function set_chargeTime(value : float) { _chargeTime = Mathf.Clamp(value, 0, 1); }

    In this case, the getter function is somewhat redundant since it doesn't do anything special that reading _chargeTime directly wouldn't do, except possibly making a private variable accessible. Using the setter function will require minor changes to the code that used to write to the _chargeTime ex-property, but if there is a lot of code to change you can generally use a search&replace to do most of it quickly and easily.
  5. GibTreaty

    GibTreaty

    Member

    Joined:
    Aug 25, 2010
    Messages:
    583
    @zine92
    That's all I needed to know.

    @dreamora
    I read that thread before I posted and was hoping for a closer C# equivalent way to do it.

    Edit <
    @Wennid
    I'll have to settle for that way, thanks ;)
    Code (csharp):
    1.     private var _chargeTime = 1.0;
    2.    
    3.     function GetChargeTime(){ return _chargeTime; }
    4.     function SetChargeTime(time : float) { _chargeTime = Mathf.Clamp(time, 0, 1); }
    >

    @Everyone
    Thanks for your help, now I can continue with the conversion. It just won't be exactly like the C# version, which I do understand that it isn't always possible to convert everything. For example, you can't convert C#'s reflection to Java.
    Last edited: Aug 6, 2011
  6. PeterB

    PeterB

    Member

    Joined:
    Nov 3, 2010
    Messages:
    98
    I'm using property getters and setters all the time in UnityScript. As far as I can see, all of the respondents above are wrong - you can use property getters and setters exactly in the same way as you do in C#. You need an explicit class declaration, though. Here's an example for a source file named Foo.js:

    Code (csharp):
    1. #pragma strict
    2.  
    3. class Foo extends MonoBehaviour {
    4.  
    5.     private var _bar : float = 0.0;
    6.     public function get bar () : float { return _bar; }
    7.     private function set bar (value : float) { _bar = value; }
    8.  
    9. ...
    10.  
    The property accessors need not both be public or private. For a read-only property, just leave out the setter definition. It's really very simple.
    Last edited: Aug 6, 2011
  7. Wennid

    Wennid

    New Member

    Joined:
    Jul 30, 2011
    Messages:
    9
    That's a bit harsh. I admit that I wasn't aware of UnityScript's getter/setters until just now (in fact I came back to this thread to post a link to another thread that mentioned them, but obv. no need for that now).
    I wasn't "wrong" - I never said UnityScript CAN'T do it, I just said that you can do it another way. I haven't followed dreamora's link, but it looks like he was pointing to UnityScript having getter/setter functionality too.
  8. Dreamora

    Dreamora

    Member

    Joined:
    Apr 5, 2008
    Messages:
    26,586
    Not all are wrong PeterB, my answer obviously just was ignored or the thread that I linked was just ignored cause the exact code you posted is in there ...
    Now I'm a sad panda ;)
  9. zine92

    zine92

    New Member

    Joined:
    Nov 13, 2010
    Messages:
    1,347
    what i meant was that get and set method is not in Unity script, didn't say javascript cannot write your own. I only said set and get is SPECIAL method already built in C# and not javascript.
  10. GibTreaty

    GibTreaty

    Member

    Joined:
    Aug 25, 2010
    Messages:
    583
    @PeterB
    Thanks, that works 100% perfectly!
  11. PeterB

    PeterB

    Member

    Joined:
    Nov 3, 2010
    Messages:
    98
    Sorry, Dreamora, if I made you an unhappy bunny - that was certainly not my intention. However, as far as I've understood it, the UnityScript property setters/getters are exactly the same as the C# ones, as they boil down to exactly the same intermediate code. I had a look through the thread you referred to before I posted, but I couldn't find the same code there. I added my example to that thread, then deleted it again, since the other thread mainly deals with static properties. And there is no mention in that thread of the need to declare the class explicitly, which is the crucial bit.

    Anyway, let's not quibble. Main point is that property setters and getters are available both in C# and UnityScript, with the same underlying implementation.

    UnityScript is still a little misunderstood, despite the fact that it's just as expressive as C# nowadays. It has full support for generics, anonymous lambdas, closures and much more. Personally I think it's much more suited to functional programming than C#. But then I am a Lisp guy at heart.

    (Which means that Boo, with its macros and its malleable syntax, is starting to look quite attractive...)
    Last edited: Aug 6, 2011
  12. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Messages:
    24,683
    Probably because it's not really properly documented....

    --Eric
  13. PeterB

    PeterB

    Member

    Joined:
    Nov 3, 2010
    Messages:
    98
    That's very true. One has to go to update notes scattered here and there. An official language specification as a section in the manual would be a good thing.
  14. Dreamora

    Dreamora

    Member

    Joined:
    Apr 5, 2008
    Messages:
    26,586
    somewhere such a section must exist as they proposed it as an open standard or at least a public one last year and that commonly requires a formal spec not?
  15. Nephilim

    Nephilim

    New Member

    Joined:
    Aug 11, 2011
    Messages:
    10
    Okay, so how do you get static getter/setter's to work, then, as was mentioned in the thread linked to above? I've tried wrapping the code in the class declaration, and static getter/setters still seem to fail.

    For instance, the below code:

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. class StaticTest extends MonoBehaviour {
    5.  
    6.     function Awake() {
    7.         StaticTest.test = "foo";  // This line throws an error.
    8.     }
    9.    
    10.     public static function set test( value:String ) {
    11.         Debug.Log( "Set the static test value to " + value );
    12.     }
    13.  
    14. }
    15.  
    ...throws the error "BCE0020: An instance of type 'StaticTest' is required to access non static member 'test'."
    Last edited: Aug 12, 2011
  16. PeterB

    PeterB

    Member

    Joined:
    Nov 3, 2010
    Messages:
    98
    The UnityScript compiler is written in Boo. Therefore, if static property setters and getters work in Boo, there's no technical obstacle to them working in UnityScript. Indeed, static getters do work:

    Code (csharp):
    1. class Aardvark extends MonoBehaviour {
    2.     static function get whatever () : int { return 12345; }
    3. }
    But, as you say, static setters do not work:

    Code (csharp):
    1. class NoWai extends MonoBehaviour {
    2.     static function set oRly (value : int) { Debug.Log("You wish!"); }
    3. }
    Googling for "Boo static properties" yields the following bug report: http://jira.codehaus.org/browse/BOO-162, which confirms that the situation is exactly the same in Boo. There's clearly a bug in Boo which hasn't been fixed.

    My guess is that this has absolutely nothing to do with the UnityScript compiler – there's a good chance that fixing the Boo bug also fixes the UnityScript compiler error.

    I'm going to file a bug report in Unity, and I suggest you do the same. I have also posted on the Boo language list.
    Last edited: Aug 14, 2011
  17. gilley033

    gilley033

    Member

    Joined:
    Jul 10, 2012
    Messages:
    211
    This still hasn't been fixed? I get "An instance of type 'ClassName' is required to access non static member 'MaxRow'.

    Here's what my class looks like (left out some irrelevant stuff)

    Code (csharp):
    1. class Grid extends System.Object
    2. {
    3.      private static var maxRow : int;
    4.  
    5.      static function get MaxRow() : int {return maxRow;}
    6.      static function set MaxRow(value : int) {maxRow = value;}
    7. }
    And then I'm accessing it like so:

    Code (csharp):
    1. Grid.MaxRow = someIntegerVariable;
    So obviously the property is static, yet it's throwing out that warning. So . . . no fix for this yet? Like Peter said somewhere else, it's crazy that this Boo bug hasn't been fixed in 7-8 years. I can only find two threads talking about this. I guess most people who use getters/setters use C#.

    If anyone has found a solution, I'd love to hear it!
  18. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Messages:
    24,683
    The best thing would be to submit a bug report, if you haven't already.

    --Eric