When running this script, the Profiler shows that deactivating Game Objects is causing a great deal of lag. The project is running better without this script at all!
At what point does my code become laggy? If it is an inherent symptom of SetActive(false), then I am not sure how to go about trying to optimise the map. I’m happy to provide more information, if needed.
If SetActive(false) is not the right way of trying to save computer resources, can anyone advise as to the most appropriate way of doing this action?
What exactly is a chunk in your context?
How many chunks are there approx. / how complex are these chunks / what’s their individual size?
Would it be possible to combine these in any way?
Where/When/How often is this method called?
Your logic appears to be much of a “brute force” attempt, i.e. you’re simply checking against everything and iterating through all the objects even though it might not even be of any interest.
There are great algorithms and data-structures that are designed to operate on subsets of a given set of data, in your case spatial datasets (your environment chunks).
That’s probably the first thing I’d look into, for instance, if you’re on the one side of the map, there’s no need to check things on the other side.
You could implement an approach based on grid-mappings if that’s an option (I don’t know anything about your environment and its design tho).
Or you might find simple examples of quadtree implementations, if you also need to consider heights, you can look into octree-structures.
And so on…
This is actually a huge topic and without further information it’s difficult to tell in more detail how you can improve your chunk management.
A chunk comprises of two different flat 16 X 16 grids, making 512 tiles. A whole chunk is 3076 KB in size.
Currently, I need each tile on the grid to know its ID and its surrounding grid IDs. This allows the Player to move correctly and grid tiles need to know whether or not there is an interactable object on its grid ID and its Terrain Type.
The method is called whenever the Player is 4 grid tiles away from the edge of the map. As the grid is quite small, the method can be called quite often. Below is a video showing chunks being generated (but does not show my attempt at optimising by turning Grids off).
I am mostly self-taught and now I have searched Quadtree Implementation, I can see that I have much to learn and will look into this. I think most of my work is brute force, so I might need to give it all a rethink.
Do you think a Flyweight Pattern might be the way to go with this project, as all grid tiles have the same type of information as one another?