- Home /
List or Array?
Hello.. I have that dilemma about using list or arrays.. Here s the context: I have those game objects randomly spawning at fixed location. I have that spawnSpots array populated with instances of a SpawnSpot prefab that are placed at different points of the map. Now, i want to prevent spawning on an already occupied SpawnSpot. My basic idea is to have SpawnSpot removing itself from the spawnSpots array when it is occupied (and re adding itself when it becomes available again). But apparently arrays contains a fixed number of elements, so it may not be easy to remove and add element to it and i should change my spawnSpot array for a list, which is supposed to be more flexible. But, also apparently, arrays are more efficient in terms of performences... So here is my question: - Should i go for a list instead of the array (according that there will never be more than, say, 16 SpawnSpots, maximum)? or stick to the array?
Alternatively, would it be possible to filter objects in the array, say by tag? (so i wouldnt have to bother about removing/adding the available objects from the array, but just tag them as "good for selection")?
And finally, please dont write me any code.. Just point me to the right direction :)
Cheers
Answer by hexagonius · Oct 26, 2018 at 08:19 PM
first of all, performance is not in the least a reason for choosing one over the other in your case. it's not a tight loop or anything. In your case it's just a matter of convenience.
you could have an array of bools in parallel you toggle. you could have a list, actually two to not forget choices... puck your favorite.
Answer by Will_Croxford · Oct 26, 2018 at 06:20 PM
Object pooling - https://www.raywenderlich.com/847-object-pooling-in-unity
See the code on this page, download his sample project. He uses a List, the key to performance is actually pre-insantiating all the objects, and not creating them and destroying them in the game. He does it in code, but you could just use the Inspector to create all the spawn points if they're in fixed positions.
you can index the objects in the array and pop and push like a stack (as in computing science definition of stack) from my memory, so may be no need to tag the objects. I think the idea is you have 16 spawn spots inactive at start of game, then activate them one by one, deactivate when you need, then if you need more than 16 at some point, you increase size of list.
Yes you can always filter by tag, if (x.tag == "spaceship") or whatever, but then you have to remember to always have the tag there, plus safety line of code in case tag missing to stop it crashing, so if you can do what you need with just the array or list index that'd be even better.
I'd google "Unity array list use cases" to see any pointers, but the idea is to use profiler when game gets to certain size (near the end stage of development I think is the idea!) so you actually see which parts slow you down, and focus on them instead of wasting time splitting hairs over a size 16 array or list. also Unity docs has a tutorial on object pooling but I couldn't see where the code is on the page, so I chose this one as all the code is neatly displayed on page. I think it's quicker with article than video in the long run to really digest the meaning of it all.
Right.. Thanks a lot!.. Plenty of new terms and concepts in your answer for me to look up.. The final answer may very well be in there, so thanks!.. Couple of things that maybe will clarify the actual situation: - there s only a fixed number of spawnSpots by map/level, according to difficulty level. Eg: 4 at low level, up to 16 at higher/ more difficult levels, but the amount of spots within each map do not change. They are not created or destroyed on the fly, they exist in each map and are dropped in as the elements of the spawnSpots array, in the map s script component, in the editor. - the game spawns at interval according to difficulty (like, every 5 sec at low level, every sec or 0.5 sec at higher level) by randomly picking one of the possible SpawnSpots of the map. - At the moment, the SpawnSpots objects can detect if they are occupied (with collider), which change their tag from "free" to "occupied" (and back if collision is null) -At the start of the game, all the SpawnSpots of that map are active.. And they should become inactive while occupied, and then become active again.. Just so that nothing spawns on an already occupied spot