- Home /
unityscript class + array problem
I want to create a class and then make an array of its objects.
If I do it like this:
class strtest
{var a:int;}
var test:strtest[]; //and later: var test = new strtest[100];
Then I don't know how to access it's variables, since
test[5].a=3;
gives "a is not a member of strtest" or so. Which as I understand it means that some pointer goes nowhere and a different syntax is required. I believe the variable should be accessible this way since I can modify it from the inspector.
Now if instead I use
class strtest extends System.ValueType
I can access it as expected BUT it disappears in the inspector, which is annoying.
Solutions, anyone?
Side question: Am I supposed to be using classes at all when working with unityscript? (Or does that go against the concept of javascript)
Thanks in advance
Don't use "arrays" - use LIst ..
note this question
http://answers.unity3d.com/questions/388075/remove-object-from-array-in-javascript.html
and many others
Answer by Loius · Jan 26, 2013 at 11:08 AM
When you
new MyType[x]
(unless you do create it as a public variable visible in the inspector), that only allocates space for X variables of type 'MyType'. You also have to create each one:
var test : TestClass[] = new TestClass[100];
for ( var ix : int = 0; ix < 100; ix++ ) {
test[ix] = new TestClass();
}
This is a thing I still get caught up on once or twice a week, after YEARS of working with it. >_<
If that's not your issue, try declaring "public var a" instead of just "var a". I'm pretty sure the default is public, but maybe there's a weird setup issue or maybe I have crazy memory.
If none of this is helpful, could you post the entire script, or make up an entire script that exhibits the problem?
Yes, you should definitely be using classes with Unityscript. I know that part of the answer's right. :)
That's basically true, but when you declare a public array in the inspector, you don't actually have to initialize the array or the classes in the array if they are serialized. The Unity editor will create them automatically.
Of course when you do
test = new strtest[100];
in Start or Awake it will overwrite the automatically generated Array + classes and you have to create the class instances manually like you said.
Oh, btw the type of "test" should be TestClass[], not TestClass ;)
Augh, thanks. $$anonymous$$y compiler keeps telling me the same thing. xD
Answer by schaddem · Jan 26, 2013 at 06:25 PM
Thank you your solution works.
So to make an array of classobjects you have to call each ones constructor indivdually after claiming its space.
class testclass //define class testclass
{var a:int;var b:int;}
var object:testclass[]; //announce array of objects
var arraysize = 1000;
Awake()
{
object=new testclass[arraysize]; //reserve space
for (var i = arraysize; i--;) {object[i]=new testclass();} //call constructors (counting down was quicker in c++, it's probably the same in unityscript)
}
As far as List() is concerned that's no replacement for built in arrays, it's a replacement for Array(). Built in arrays are fastest and not resizeable, List is somewhat slower(I've read about 50-60%, but not sure if thats correct) and resizeable, everything else should, according to public opinion, not be used (that includes java Array(), List is superior).
That said, List might save you running through empty entrys and might thus be faster.
@bunny: I don't understand your comment. If we dont call
object=new testclass[arraysize]; how is unity supposed to know the size of the built in array? Is there some way we can define an array of classobjects with less code and let unity do the work for us?
(that's not an answer; it should be posted as a comment)
If you have this:
class X extends $$anonymous$$onoBehaviour { var x : TestClass[]; }
You can change the size and values of x in the Inspector, and you don't have to initialize it in code (Unity's actually doing all the initialization and stuff for you as you resize it in the inspector)
Thanks, good to know.
Regarding speed comparision between List and .net Array, I've done some more research:
List/for: 1971ms (589725196)
Array/for: 1864ms (589725196)
List/foreach: 3054ms (589725196)
Array/foreach: 1860ms (589725196)
If this test is sound (it probably is) then we're talking about $$anonymous$$ARGINAL speed differences, something like 5%. In return we gain comfort and scaleability, and if we've got even a small amount empty listentries List() could easily be faster(if we run list vs list we could end up vs emptylistentries*emptylistentries = waste).
So now I believe that we should be using List for pretty much everything and inbuilt arrays only as a microoptimization for arrays with fixed size.
I'm a little bit worried about resource allocation at runtime though.
Your answer
Follow this Question
Related Questions
Error: "ArgumentException: get_deltaTime can only be called from the main thread" 1 Answer
How does UnityScript class scope work, when in same file? 0 Answers
Javascript / Unityscript adding elements of an array 2 Answers
JS to C# class conversion issue. 1 Answer
Creating a dynamic array of objects of custom class 1 Answer