Is there a way to describe the type of the objects contained within an array? I have an array of a class I call "Item", with itemType as a variable within the class Code (csharp): ItemArray[Item_1].itemType This code produces this error Assets/menuSystem.js(891) error BCE0019: 'itemType' is not a member of 'Object'. Is this a totally trivial problem?
Sorry, I wasn't clear... I'm using dynamic arrays. Do they only take type 'object'? If so, I find it odd that a dynamic array should cause pragma strict to fail if there is no way to declare type, short of using static arrays, of course.
you should be able to do something like this: var theItemArray : Array = new Array(); theItemArray.push(theItem); var theItem : Item = theItemArray[0]; print(theItem.itemType);
hmmm, if that's the case, I'm probably not gaining anything in terms of performance over leaving it as dynamic typed.
Well yes you will gain some performance. Code (csharp): // Dynamic typing some_array[2].DoSomething(); // vs. static var item : TheType = some_array[2]; item.DoSomething(); The benefit from static typing comes from that in the examples above, the DoSomething method has to be looked up by name at runtime in the dynamic case, but is already resolved at compile time in the static case. This has nothing to do with the values being temporarily cast to Object while inside the array. It's not until you try accessing member variables or calling methods on the objects the actual type information is needed.
thanks for the clarification keli-- I'd recommend including that in the performance optimization section for dynamic vs static type--at the very least for programming n00bs like me. It's a huge help when you guys explain why these things are so (as you just did), particularly for people new to scripting--it makes everything feel less arbitrary.