- Home /
Will a large array of game objects slow my game down?
I'm working on a 2D, top-down action adventure game. It occurred to me today that it could be very useful to use a large 2D array of game objects to represent each level, where each element in the array represents an integer value on a grid used to place objects. For instance, this could be used in collision detection: the moving object could check the array elements adjacent to them, and as long as they are null, they are free to move. It could also be used as a very fast alternative to raycasting.
My concern is that these arrays could be fairly large, potentially containing hundreds or even thousands of elements. I don't THINK that's big enough to cause me to bump into memory issues or slow-down, but before I go about implementing it I wanted to see what other people thought. Does this idea require too much space to be viable?
A better way would be to keep the array internal, so no actual GameObject for each grid position, but rather instances of a non-$$anonymous$$ono class that holds thre required data.
Arrays of objects are effectively arrays of references to objects. They'll probably be 64 bit each (http://stackoverflow.com/questions/3800882/how-big-is-an-object-reference-in-net), so even with a few thousand of them I wouldn't worry too much about the size of the array, more about the size of the objects it will reference.
Lookups in arrays are also pretty cheap. Where you might run into performance problems with your approach is if you want to start going over the elements.
Answer by guido-paglie · Oct 15, 2015 at 09:45 AM
Hi! This way you are talking about are made lots of games, as tetris, bomberman, etc. In one side you have the logical part, where each index represents each thing, for example in bomberman 0: indestructible block (up down left down walls) 1: grass (bomber man can move) 2: destructible block (it can be explode) 4: bomb 5: explosion (if an explosion goes to a cell that there is a index == 2, you can destroy the content ;)
in a similar way tetris is done, 0 for the cells without blocks, 1 with so if you add a block, and then you check all the 2d array and you have one line all complete of 1, you can do what tetris does... eliminate the graphics of the line ( in the logical part you will put al 0, you will push down all the lines above that destroyed line, etc..)
BUT, this have 2d arrays very small, tetris might be 7 * 25, and bomberman 30*30. But you are asking for something max of 1000*1000. Question: the map is going to be that big?? because maybe you can divide it into scenes, or you can have only rendered the 2d cell inside this big 2d cell, that the camera is seeing, i mean the portion that the camera is seeing OR only travelling the part of the 2d cell that the camera is seeing, or where the player or stuff is happening.
Anyway, 1000*1000 is too big, give it a try (better no...), 2 for i < 1000, j < 1000. and surely unity is going to crash :$. if you have i < 100, and j < 100, it does take some few seconds, so i thing you will have to think in a walk around.
hope it helps, cheers!
Answer by sandeepsmartest · Oct 15, 2015 at 05:36 AM
Yes,very large array of game objects will definitely effect your game performance.There will be a situation where you need to check the objects in the array very frequently or atleast 3 or 4 times for a single session , this makes the game performance very low coz you can check the array using for-loop which loops the array in every index. It is fine if you just place the gameobjects in array.If you once start checking or do some operations on the array will definitely cause performance issues. You may get exceptions like null reference if you destroy the gameobject that belongs to array.it would be bit tough to handle these kind of issues. Hope this may help you. Nsks
Your answer
Follow this Question
Related Questions
JavaScript 3 Arrays questions 1 Answer
Array of Arrays of GameObjects/Prefabs (C#) 1 Answer
Is it possible to store variables in arrays? 1 Answer
Destroy created children of objects 1 Answer