How do you make a struct in JS? Does this work? Code (csharp): var myArray = new Array (); struct myStruct { name : String; amountOfSomeThing : Float typeOfObject : String } myStruct.name = "laser"; myStruct.typeOfObject = "weapon"; myStruct.amountOfSomeThing = 100.0; myArray.Push (myStruct);
We just do: Code (csharp): class myStruct extends System.Object { var name : String; var amountOfSomeThing : Float var typeOfObject : String }
As far as I know it doesn't. Explicitly inheriting from System.Object will prevent JavaScript from trying to inherit from MonoBehaviour.
System.Object isn't a struct though, you can accomplish the same just by: Code (csharp): class myClass { var name : String; var amountOfSomeThing : Float var typeOfObject : String } Difference between classes and structs are that classes are allocated on the heap and act (well, are ) 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:
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.
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): var myArray = new Array (); class myStruct{ var name : String; var amountOfSomeThing : float ; } function Start(){ myStruct.name = "shot gun"; myStruct.amountOfSomeThing = 10.1; myArray.Push (myStruct); print(myArray[0].name) }
You could use a variable. Code (csharp): var myArray = new Array (); var struct = new myStruct (); class myStruct{ var name : String; var amountOfSomeThing : float ; } function Start(){ struct.name = "shot gun"; struct.amountOfSomeThing = 10.1; myArray.Push (struct); print(myArray[0].name) }
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
"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...