x


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?

more ▼

asked Sep 29 '11 at 03:58 AM

stereosound gravatar image

stereosound
33 5 8 9

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

@stereosound

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).

more ▼

answered Sep 29 '11 at 05:17 AM

Julien.Lynge gravatar image

Julien.Lynge
7.4k 20 25 50

Thanks for the quick response! Is loading from an XML 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!

Sep 29 '11 at 05:23 AM stereosound

Reading the xml isn't a huge hit, it just requires some extra disk space / memory (see http://forum.unity3d.com/threads/45447-XML-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.

Sep 29 '11 at 05:45 AM Julien.Lynge
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3446
x352
x288

asked: Sep 29 '11 at 03:58 AM

Seen: 960 times

Last Updated: Sep 29 '11 at 05:45 AM