Search Unity

JS and Structs

Discussion in 'Scripting' started by Sync1B, Jul 3, 2008.

  1. Sync1B

    Sync1B

    Joined:
    Sep 13, 2005
    Posts:
    561
    How do you make a struct in JS? Does this work?


    Code (csharp):
    1.  
    2. var myArray = new Array ();
    3.  
    4. struct myStruct {
    5.        name : String;
    6.        amountOfSomeThing : Float
    7.        typeOfObject : String
    8. }
    9.  
    10.  
    11. myStruct.name = "laser";
    12. myStruct.typeOfObject = "weapon";
    13. myStruct.amountOfSomeThing = 100.0;
    14.  
    15.  
    16. myArray.Push (myStruct);
    17.  
    18.  
     
  2. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
    We just do:

    Code (csharp):
    1.  
    2. class myStruct extends System.Object {
    3.        var name : String;
    4.        var amountOfSomeThing : Float
    5.        var typeOfObject : String
    6. }
    7.  
     
  3. Sync1B

    Sync1B

    Joined:
    Sep 13, 2005
    Posts:
    561
    Can you explain that to me a little? Does JS not have structs?

    Bill
     
  4. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
    As far as I know it doesn't. Explicitly inheriting from System.Object will prevent JavaScript from trying to inherit from MonoBehaviour.
     
  5. bronxbomber92

    bronxbomber92

    Joined:
    Nov 11, 2006
    Posts:
    888
    System.Object isn't a struct though, you can accomplish the same just by:

    Code (csharp):
    1. class myClass {
    2.        var name : String;
    3.        var amountOfSomeThing : Float
    4.        var typeOfObject : String
    5. }
    Difference between classes and structs are that classes are allocated on the heap and act (well, are :p) like reference types, and structs are allocated on the stack and are value type object. Check google for a more elaborate description ;)

    As for answering your question, I don't know :roll:
     
  6. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
    The problem is if you have a file, MyInfo.js, and do a class MyInfo { } declaration inside it, Unity will still automatically inherit from MonoBehavior. We were told the simple solution is to explicitly extend System.Object.

    This may have been fixed since (I think this around the original 2.0 beta timeframe)...

    Edit: Looks like it's fixed! Well, old habits die hard.
     
  7. Sync1B

    Sync1B

    Joined:
    Sep 13, 2005
    Posts:
    561
    I get a error saying that an instance of type 'myStruct' is reaquired to access non static member 'name'. How do I instance myStruct? I thought thats what I did in the Start().

    Code (csharp):
    1.  
    2. var myArray = new Array ();
    3.  
    4. class myStruct{
    5.        var name : String;
    6.        var amountOfSomeThing : float ;
    7. }
    8. function Start(){
    9.     myStruct.name = "shot gun";
    10.     myStruct.amountOfSomeThing = 10.1;
    11.  
    12.     myArray.Push (myStruct);
    13.         print(myArray[0].name)
    14. }
    15.  
    16.  
     
  8. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    You could use a variable.

    Code (csharp):
    1. var myArray = new Array ();
    2. var struct  = new myStruct ();
    3.  
    4. class myStruct{
    5.        var name : String;
    6.        var amountOfSomeThing : float ;
    7. }
    8. function Start(){
    9.    struct.name = "shot gun";
    10.    struct.amountOfSomeThing = 10.1;
    11.  
    12.    myArray.Push (struct);
    13.         print(myArray[0].name)
    14. }
     
  9. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,066
    As a work-around, both Boo and C# do support struct and if the script files are put inside the Standard Assets folder, those structs can be used from JavaScript.

    http://boo.codehaus.org/Part+11+-+Structs
    http://msdn.microsoft.com/en-us/library/aa288471(VS.71).aspx
     
  10. bronxbomber92

    bronxbomber92

    Joined:
    Nov 11, 2006
    Posts:
    888
    "myStruct" (which is actually a class), is a type, not an object/instance. What you're trying to do is the equivalent of Vector3 = Vector3.zero; myArray.Push(Vector3). You must create an instance (ie - variable) of it:
    myStruct myStructVariable = new myStruct();
    myStructVariable.name = "shot gun";
    ect...