So I’ve got a map generator that makes a huge map of a cave (~1000-3000 tiles^2) by referring to a randomly generated 2D array built out of 0s and 1s. It actually does this relatively quickly; my problem is with managing memory. Right now, I just have an entity create a tilemap of the desired size, write the tiles from the array, and then set its graphic to the tilemap (which is where the memory is wasted). After it does this, the game itself takes up 200-500mb of memory. How do I go about converting the tilemap into a bitmapData so I can break it down into loadable chunks? Any other strategies for breaking this up so the tilemap is easier on the memory would be appreciated. Without creating and loading the tilemap, the game only consumes ~10mb (as expected).
Managing a huge tilemap?
The obvious strategy is to generate chunks on demand. For example, each chunk can has a size of 50x50 tiles. Whenever the player is near to its border, you generate a new chunk.
On the GBA we used to stream tiles at the edges of the screen. Basically, when the player had moved 16 pixels, we’d read another line of 16x16 tiles along the edge. To the player, it’d look like a freaking massive world… but there was only ever actually a screen worth plus a strip on each edge worth of tiles in existence at any given time.
You could probably get away with refreshing the whole screen + strips worth of tiles to make the code a little easier, rather than just doing the edges (which produced some rather messy but highly efficient code).
There is something seriously wrong here. Total mem should take size of array and thoese two tiles (since it’s 0s and 1s only). Did you try using Tilegroup?
I didn’t even know tilegroup was a thing. Would I write the map straight into a tilegroup instead of using flashpunk’s regular tilemap functions?
Looks like it. It basically looks like it stores your map in a 2-dimensional vector instead of a bitmap to save memory, then renders a screen + strips as I described before. It should save some memory over using the bitmap like Tilemap does.
One thing I am wondering is how this saves memory. In theory a 4096x4096 tile map should take up the same amount of memory whether it’s stored in a 2d vector or a 2d bitmap, because they’re both still using 32bits of a data per tile…
hey rostok im trying to play around with this code but it gives me a “cannot access a property or method of null object reference” at the
columns = tileset.width / tileWidth;
i think it might have to do with my source being incorrect. i’m trying to pass an XML should my source be an array instead?
Looks like your tileset is null. I set this up by giving Tilegroup’s contructor a BitmapData reference, and loaded this manually with loadFromString method. It’s not in the version I pasted before (didn’t know there were changes). Here it is http://rostok.3e.pl/download/Tilegroup.as
so you would trace a tile map as your source? i don’t get any errors when I try that but nothing appears on the screen I guess i’m dumb
I’d like to help you but franky I used this TileGroup only once and my code is quite a mess. However you can check the github repo from the link above - maybe there’s some working example of how this should be set up.
What do you mean by ‘tracing a tile map as source’? If the ‘source’ is info on positions of particular tiles then you just do it manually by setting XML or string. I guess the format for loadFromString() goes like this:
0,0,0,0,1,1,1,
0,0,0,0,1,1,1,
0,0,16,0,1,1,1,
If this wont solve you problem post your code here or just debug the update() and render() methods.