Random tile based levels and avoiding GC

Hi,

For a next project, I will have to make random tile-based levels. It’s mostly horizontal sideways scrolling, without the ability to go back.
I can come up with the algorithms to make playable random levels (have done this before) but I can’t find a good way to do it with a tile-based approach while avoiding GC. This is probably because I still don’t understand memory management completely.

In order to get good performance, I was planning to combine the tiles into large chunks. Each chunk will just be a mesh with the vertices and uv’s in the right place.

I’ll have a few of those on screen, and once one leaves the screen, it will be placed on the other side and regenerated.

Now here’s the problem : if I regenerate the mesh, I will have to make new arrays for the vertices and uv’s. Won’t these arrays increase the heap, and thus invoke GC after a while?

Maybe I can reuse the arrays too, but they will have different sizes because they can have a different number of tiles. But maybe I just have to fill the unused part of the arrays with values that are always off screen?

Can anyone with a better understanding of memory management help?

Thanks!

If you create a new array each time you alter the mesh, you will indeed create a bit more garbage. Allocating arrays large enough for the worst case and hiding the unused parts offscreen when reusing them will reduce the number of fresh allocations.

You could also do what I do on the XBox 360 (which has horrible GC) and that is to create pools of memory or objects that you just re-use.

Never allocate anything dynamically is my mantra in game development. For example, don’t call new on your level array every scene, just change the active flags on your tiles and turn off the ones you aren’t using. Change the textures, materials, and even meshes but don’t allocate or create new ones.

It’s a method that’s been used for as long as I know game development has been around. I recall using my own memory pools back in the DOS days when we only had 640k total. :wink:

Yeah, I know I have to reuse stuff, I have my own pool classes and all that :slight_smile:
It’s just that I can’t have a pool of tiles because I want to combine tiles in one mesh, but it can be a different amount each time.

So I guess the only solution seems to be creating the worst-case arrays like I first suggested and like andeeee confirmed, unless anyone has a better idea.

Thanks for the replies!