Search Unity

Static getter/setter

Discussion in 'Scripting' started by BerzerkLachhh, Sep 6, 2010.

  1. BerzerkLachhh

    BerzerkLachhh

    Joined:
    Jun 24, 2010
    Posts:
    28
    In JS, When I create static getter/setter, it always gives me this :

    Assets/src/com/homerun/init/HB_VersionInfo.js(167,32): BCE0020: An instance of type 'HB_VersionInfo' is required to access non static member 'test'.

    Which makes no sense, since the setter is static.

    Code (csharp):
    1. static public function set test(value:int) { _anotherValue = value;}
    This method works if it's not static, but obviously not what I want. Pretty annoying.

    Have an idea ?
     
  2. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
    Is _another value a static variable too ?
     
  3. BerzerkLachhh

    BerzerkLachhh

    Joined:
    Jun 24, 2010
    Posts:
    28
    Yup,

    In fact you could just try :
    static public function get test():int {return 0;}
     
  4. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
    I think the space is causing the problem. Rename the function Set_Test, or SetTest.
     
  5. BerzerkLachhh

    BerzerkLachhh

    Joined:
    Jun 24, 2010
    Posts:
    28
    Removing the space will make it a function, not a getter/setter.

    Edit : I.e
    Code (csharp):
    1. MyClass.setTest(value) ;
    instead of this expected result :

    Code (csharp):
    1. MyClass.test = value;
     
  6. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    are you on u3 beta6+?

    otherwise there is definitely no support for anything like properties / getter setter independent of how you do it.
    unsure how it works on the new UnityScript as per beta6+
     
  7. BerzerkLachhh

    BerzerkLachhh

    Joined:
    Jun 24, 2010
    Posts:
    28
    Yeah, I'm using u3 beta 7. I'll check out in the release note or something to see how it work then.

    I'll post back if I find something.

    Thanks !
     
  8. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    For completeness:

    On C# it would look like

    Code (csharp):
    1. class test {
    2.   private static int bla;
    3.   public static int Bla {
    4.     get { return bla; }
    5.     set { bla = value; }
    6.   }
    7. }

    perhaps that helps
     
    ow3n likes this.
  9. BerzerkLachhh

    BerzerkLachhh

    Joined:
    Jun 24, 2010
    Posts:
    28
    Yeah, I saw that it works on C# in SM2 scripts, that's why I assumed it would also worked on JS. and since the error isn't "syntax error" or something, I guess this will be fixed in the last Unity 3.

    A similar issue is doing getter/setter in interface, it just don't work either. :(. Or simply doing interface ! The name of the interface must not equal the name of the file otherwise it don't work. Hope all those things will be fixed...

    Anyway thanks !
     
  10. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
    ?

    I thought getter / setter were just mere functions that return or define a variable value, like

    Code (csharp):
    1. function GetHealth () { return health; }
    2.  
    3. function SetHealth (value : int) { health = value;}
    Have I missed something ?
     
  11. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Nope, thats what they do actually.
    They are just a more convenient way cause you no longer call them as functions but through "regular value assignements".
     
  12. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
    Ok...but the space in the name function ? Is it ok ?
     
  13. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    nope thats not ok and I've no direct idea where such syntax could come from.
     
  14. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    When you talk about the space in the function name, you mean when it was written like:

    "public function set SomeProperty"

    If so, that's likely coming from Flash's ActionScript. ECMAScript/JavaScript (and therefore UnityScript?), as far as I know do not have getters/setters/properties. ActionScript, based on ECMAScript, added that get/set syntax.
     
  15. BerzerkLachhh

    BerzerkLachhh

    Joined:
    Jun 24, 2010
    Posts:
    28
    I'm using u3 beta 7, and that get/set syntax works on object instance. No problem at all. But only static one doesn't seems to work. Maybe it's a feature from mono 2.6, dunno...
     
  16. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    UnityScript is no mono language, its a unity only thing
     
  17. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    Ahh, well if they've added that syntax to support instance-level properties, it'd make sense that they'd support static-level properties as well. Guess that should be forwarded to the Unity team to update their UnityScript compiler.
     
  18. BerzerkLachhh

    BerzerkLachhh

    Joined:
    Jun 24, 2010
    Posts:
    28
    Sent a bug report on this !

    Thanks all
     
  19. PeterB

    PeterB

    Joined:
    Nov 3, 2010
    Posts:
    366
    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, indeed, 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