So I'm working on a game where there is a forest.
I have a (currently 80x80) array that stores whether there is a tree or not in each cell. Trees are static, solid objects.
Originally I had the whole forest be built at the start of the game but I noticed in the inspector that CPU usage was very high, presumably because of the amount of objects (approx. 2000). However I was able to maintain a pretty consistent 60fps on my older machine with no obvious stuttering.
However I ideally wanted the forest to be bigger and that became very problematic performance-wise. So I thought I'd be clever (uh oh) and have the trees 'stream' in from the array only when the player was near to them.
My initial crude go at this was:
Every 0.5 seconds --- Destroy all Trees
--- For each XY Element in Array
------ If CurValue is 'tree' and distance from CurX,CurY to player X,Y < 400 --- Create object Tree at that location
Now in the inspector the CPU usage is so much better however the framerate is worse. There is now a noticeable stutter every time this loop runs, i.e. every half a second.
I've deleted parts of this to check and it appears to be nothing to do with the destruction and creation of the trees and purely related to looping through the array.
Is there a better way to do this loop? Or even a better approach than arrays in this instance?