- Home /
Referencing data in pre-defined array
So, I a game where it would be ideal to call up characteristics from a pre-defined list, such that if I have GameObject1, I can go to Array[1,2], Array[1,3], etc to pull out its characteristics.
That said, I do not know #1 how to do this (does unityscript have a 2d array function?) and #2 how I can save this information in say, the editor, so I don't have a script in mono that is essentially unnecessary load times seeing as it would never change during runtime. Are there any solutions available in unity for this? Or if not, what is the best work-around?
Answer by Julien-Lynge · Sep 29, 2011 at 05:17 AM
Unityscript supports regular Javascript arrays (according to this page), so here's how you'd set up the multidimensional array in code.
In order to save the data outside of runtime, you'd have to set up an editor script to collect the data and save it to disk (perhaps in an xml document in Resources) OR set up a custom EditorGUI that would allow you to enter the data as a series of one-dimensional arrays, or other equally exotic solutions.
That being said, if you're really set on manually setting up the array from the editor you probably want to simply flatten the array to be one dimensional (and make sure it's public) and then convert it to a 2d array at runtime (the cpu time to copy an array of pointers at runtime will be absolutely trivial, on the order of nanoseconds).
Thanks for the quick response! Is loading from an X$$anonymous$$L file a noticeable speed hit? If so, perhaps it is better to just populate the arrays in a public var and merge them at runtime -- thanks for the tip!
Reading the xml isn't a huge hit, it just requires some extra disk space / memory (see http://forum.unity3d.com/threads/45447-X$$anonymous$$L-Parser-in-C). However it's only really good for saving things like ints or strings - if you're wanting to save references to components or game objects (like it appears you want to do) you pretty much need to set it up with public variables through the editor gui for your script. The overriding reason for this is that you're then linked with the library backend to Unity, so if you change a game object's name or components (for instance) your connection to the object isn't hosed. So the custom EditorGUI would work for you, as would using single-dimensional arrays and merging at runtime.
Your answer
Follow this Question
Related Questions
JavaScript 3 Arrays questions 1 Answer
Why can't I push and pop from string array? 1 Answer
What the heck is a 'function(System.Array): void' Type? 2 Answers
Javascript / Unityscript adding elements of an array 2 Answers
My array does not update when object is destroyed. How do I fix it? (java) 2 Answers