Unity Community


Results 1 to 18 of 18

  1. Location
    USA
    Posts
    558

    UnityScript Getter Setter : Are they supposed to work?

    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:  
    1.    float _chargeTime = 1;
    2.  
    3.     public float chargeTime {
    4.  
    5.         get { return _chargeTime; }
    6.  
    7.         set { _chargeTime = Mathf.Clamp(value, 0, 1); }
    8.  
    9.     }

    UnityScript

    Code:  
    1.   private var _chargeTime = 1.0;
    2.     var chargeTime : float {
    3.         get { return _chargeTime; }
    4.         set { _chargeTime = Mathf.Clamp(value, 0, 1); }
    5.     }
    YounGen Tech Website
    Projects Page (Facebook)
    - For random updates on my various projects
    Asset Store Page - All of my Asset Store packages


  2. Location
    Singapore
    Posts
    1,347
    Nope. The Get and Set is special method in C# which is just not supported in javascript and certainly not Unity javascript.
    I don't break rules. I bend them.


  3. Location
    Zürich, Switzerland
    Posts
    26,572
    The thing you mention there is C#*and called Property. It has no counter part in UnityScript

    But UnityScript has its own Getter and Setter where its a "function attribute" basically.

    See http://forum.unity3d.com/threads/596...-getter-setter for example


  4. Location
    Canberra, Australia
    Posts
    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. Location
    USA
    Posts
    558
    @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:  
    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 by GIB3D; 08-06-2011 at 03:50 AM.
    YounGen Tech Website
    Projects Page (Facebook)
    - For random updates on my various projects
    Asset Store Page - All of my Asset Store packages


  6. Location
    Stockholm
    Posts
    95
    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:  
    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. ...

    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 by PeterB; 08-06-2011 at 04:26 AM.


  7. Location
    Canberra, Australia
    Posts
    9
    Quote Originally Posted by PeterB View Post
    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
    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. Location
    Zürich, Switzerland
    Posts
    26,572
    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. Location
    Singapore
    Posts
    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.
    I don't break rules. I bend them.


  10. Location
    USA
    Posts
    558
    @PeterB
    Thanks, that works 100% perfectly!
    YounGen Tech Website
    Projects Page (Facebook)
    - For random updates on my various projects
    Asset Store Page - All of my Asset Store packages


  11. Location
    Stockholm
    Posts
    95
    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 by PeterB; 08-06-2011 at 08:31 AM.

  12. Volunteer Moderator
    Posts
    23,686
    Quote Originally Posted by PeterB View Post
    UnityScript is still a little misunderstood
    Probably because it's not really properly documented....

    --Eric
    SpriteTile: new tile system that works seamlessly with Unity 4.3 sprites
    FlyingText3D: dynamic 3D text with TTF fonts | Vectrosity: fast & easy line drawing
    Nifty utilities! Stitch terrains together - runtime model importing - file browser - fractal landscapes


  13. Location
    Stockholm
    Posts
    95
    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. Location
    Zürich, Switzerland
    Posts
    26,572
    Quote Originally Posted by PeterB View Post
    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.
    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. Posts
    10
    Quote Originally Posted by PeterB View Post
    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.
    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:  
    1. #pragma strict
    2.  
    3. class StaticTest extends MonoBehaviour {
    4.  
    5.     function Awake() {
    6.         StaticTest.test = "foo";  // This line throws an error.
    7.     }
    8.    
    9.     public static function set test( value:String ) {
    10.         Debug.Log( "Set the static test value to " + value );
    11.     }
    12.  
    13. }

    ...throws the error "BCE0020: An instance of type 'StaticTest' is required to access non static member 'test'."
    Last edited by Nephilim; 08-12-2011 at 09:34 AM.


  16. Location
    Stockholm
    Posts
    95
    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:  
    1. class Aardvark extends MonoBehaviour {
    2.     static function get whatever () : int { return 12345; }
    3. }

    But, as you say, static setters do not work:

    Code:  
    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 by PeterB; 08-14-2011 at 03:54 AM.


  17. Location
    Florida
    Posts
    184
    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:  
    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:  
    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. Volunteer Moderator
    Posts
    23,686
    The best thing would be to submit a bug report, if you haven't already.

    --Eric
    SpriteTile: new tile system that works seamlessly with Unity 4.3 sprites
    FlyingText3D: dynamic 3D text with TTF fonts | Vectrosity: fast & easy line drawing
    Nifty utilities! Stitch terrains together - runtime model importing - file browser - fractal landscapes

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •