I have huge tile maps so I separated them into small chunks (96x96 tiles). The whole idea of having chunks is that you don’t have all of them loaded at the same time, only the ones that are near the player. The problem I am facing is that I must load the world from a file, this file contains the general data from the world and the array of chunks with all the tiles info. But when I need to load a chunk, I read the entire world file, which means that all the other chunks are loaded into the memory too, so I can get the chunk I need and throw it away.
How can I load only a portion of the world file? I searched and found this answer but I have no idea how to use the
ExclusiveContentManager to load only the necessary chunk.
I understand your problem, but is it really that bad to load the whole tilemap? I mean loading it is not the problem right, but you dont have to render them all. What about if you just load the whole tilemap data into memory but only draw the tiles near the player. Everyone today has enough memory to store the tilemap data in memory, but what you draw is what makes the difference.
If you still want to just load a part of it, there is probably no way to do this with default ContentManager. Either you implement your own ContentManager which makes it possible to read files only partially, or you modify your tile map format into the chunks, create a custom importer/reader/writer/processor which reads the chunks and you create a chunkmanager combining those…
I think of it like that if your tile maps name is for example “Town”:
That would be a 2 chunk tile map. Now lets say your tilemap is 64 by 32, each chunk has 1024 tiles.
I think you get the point.
Edit: The thing is when you want to load chunks when game is running you would need asynchronous content loading, I read here on forums already that its theoretically no problem, but it is also not designed for it.
is it really that bad to load the whole tilemap?
This is how I am currently doing it. I can’t say it is good or bad because the project is still in early stages, so the world file is around 20MB, but I expect it to get bigger than 100MB.
I thought about creating one file for each chunk but it would make the things very inconvenient: if you wanna share your world with someone, you have to zip all the chunk files instead of a single world file.
The custom content manager is indeed the way to go, but I have no idea how to read only the chunk of index x in the list because to have the list in first place, I have to parse the entire file.
My Tilemap is also represented into Chunks ( 32x32 ) what I do is take the Chunk file approach “Chunk[X,Y].chunk” I have a save folder system for game saves. So a save is basically a Directory. Inside the save folder I have a Chunks folder where all the .chunk files are loaded and saved. So if you want to share a save or a world file (I have a .world file as well which contains a path to the Chunks folder that it uses) all one has to do is zip a .world file and a Chunk folder. It works well for me. Especially since they are binary files.
You can also just zip the Save folder too and just share that. But all the separate parts that make up a Save are independent from one another so you can just send the Tilemap or just send all the Entities and replace a folder or file and it loads it in no problem.
If you take this approach and use either BinaryFormatter or https://msgpack.org/index.html you can async load/save the files very quickly. I at one point didnt even need to do it asynchronously. It was fast enough without it.