- Home /
How to transform arrays into easier to edit list (JS)
I want to use this code to run gui elements and when arrows spawn/despawn in the game.
class StageEditor {
var story: String;
var controlArray: String[];
var taskName: String[];
var taskStatus: Texture2D[];
var PC: boolean;
var arrowArray: GameObject[];
}
var stage : StageEditor[];
The problem is if i want to add or change any of the stages its a ton of work going through and changing each element in multiple arrays. I think an easier way to do this would be to put this in a list but idk how to do that. Or if theres an even better way to go about making this easier to add/subtract/change stages in the middle then using a list i'd love to hear it!
generally use "List", never use arrays
(if you are a beginner, and the whole point is to learn to do good engineering)
follow the many explanations below on using "List"
"never use arrays" - If someone doesn't need to add/insert/remove, etc I don't see why he would not use normal/standard arrays, they're fast and efficient. They're the good old arrays :) But I think you meant ArrayList
?
vexe, I think it's sound advice for beginners to use only List, and not use any form of arrays, js arrays, etc.
there is just an incredible, staggering confusion from beginners here on sundry different aspects of using arrays.
All of this can usually be instantly avoided with the four word solution "learn to use List"
I mean, we use incredible abstractions that use vast amounts of processing power (like "String") all the time. I'd rather just be making machine code, I despise high-level languages, but here we are.
It is completely totally pointless, a hobbyist beginner, trying to figure out how to use arrays on computers.
I mean - why not just say "oh, don't use Unity, learn properly to make native XCode projects."
So that's my viewpoint.
Hmm, interesting view. If by beginner you meant both a Unity and program$$anonymous$$g beginner, or a 'hobbyist' like you said, then yes, I think it's faster for him to go with just lists because a hobbyist normally cares about getting things done no matter how, he generally wouldn't care about details...
But, if that beginner is a Unity beginner (came from a program$$anonymous$$g background) or someone who has just started everything and wants to make games as his career, I always encourage deep understanding of all what's happening, how things operate behind the scene, how does THIS work etc. Then, you start to get more control and flexibility over what you're doing and the innovative you become. It just feels great to understand stuff, like how does a list work from within, and how it is that the array name is actually a constant pointer to its first element, what happens when a function gets called (stack stuff) etc All that good stuff...
This is why it is much better for a new-comer to learn and be very good at program$$anonymous$$g first, and then hop in to Unity. I started answering questions about, 2 weeks ago? And all the karma I have now is 75% program$$anonymous$$g knowledge, 25% Unity knowledge.
That's my viewpoint.
right, I mean RAW BEGINNER. 95% of the questioners on this site are RAW BEGINNERS.
Answer by Tarlius · Sep 17, 2013 at 07:34 AM
It sounds like your problem is more to do with removing/moving stages around your array than the use of an array/list.
I believe what you are looking for is "custom inspector" classes. Basically you can write code that will replace/append the inspector view. For example, you could write a button that will delete a stage then do the tidy up for you. If you look at the documentation it should get you started.
You will basically want to do something along the lines of for(int i = 0; i < stage.Length; i++) { if(GUILayout.Button("Remove")) RemoveLogic(i); }
. As others have mentioned, actually implementing the logic will be easier if you use a List, since it comes pre-bundled with Remove and Insert logic.
On an unrelated note, it is good practice to give arrays plural names. Eg: stages, taskNames, etc. This lets you make nice easy to read for-loops with foreach(taskName in taskNames) {}
. Note that you can't edit a list while you are foreach-ing over it though
this looks very interesting. So with this i could encapsulate each stage and have an add/edit/delete button in the custom inspector?
I'm not sure wheat you mean by encapsulate in the context. Since the stages are already their own class are they not already encapsulated?
But yes, you could quite easily implement an add/edit/delete button in the inspector using only built in List or System.Array functionality via a custom inspector
Answer by vexe · Sep 17, 2013 at 07:14 AM
Everything you need about lists, arrays, etc in JS or C# is here.
Couple of notes:
Do your best avoiding
ArrayList
, use a genericList
instead.Whenever you have the chance to use an array instead of a list, do it.
Here's how you declare a list in JS:
var myList : List.<myType> = new List.<myType>();
Short summary of what you could do with it now:
myList.Add(element); // adds it to the end of the list
myList.Insert(atIndex, element); // inserts 'element' at 'atIndex'
myList.Remove(element); // finds 'element', and removes it from the list
myList.RemoveAt(index); // removes the element at 'index'
myList.Clear(); // removes all elements
myList.Contains(element); // returns true if 'element' is inside myList
myList.Sort(); // sorts the list elements
myList.Count // returns how many elements inside the list
Of course, when you add, remove, insert etc all memory allocations/de-allocations are done for you.
Answer by raimon.massanet · Sep 17, 2013 at 07:14 AM
Take a look at:
http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use%3F
As pointed out in:
http://answers.unity3d.com/questions/165975/javascript-list.html
you were 0.00000001 sec faster! (the time took me to fetch the link from my bookmarks)
can you make a list with multiple types? like item 1 would be a string, item 2 a float, item 3 texture2d... etc.
If you use an ArrayList, you can have multiple types for the different elements. However, as pointed out by @vexe you should avoid that if possible.
Yeah you probably don't want to do that. There would be no clean way of knowing what any particular element of the list would be- much better to simply designate seperate lists for each type.
Your answer
![](https://koobas.hobune.stream/wayback/20220613114324im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Java Script command list 3 Answers
FindIndex in a List 1 Answer
How to make a List Constructor? 1 Answer