Level Loading optimization

Hi. I made a system where by I can export a level to a text file (i write properties of each object into a text file). It looks something like this image

I have also made a system to read that file in and generate all the corresponding nodes in a scene. Here is where the problem lies. It is pretty slow, and I do not know what is the right way to optimize it. I figured that the actual reading process of the file is instant, but that the rendering/drawing of the sprites is slow (because they are drawn all at once).

I tried multithreading with std::thread but I quickly realized that CCRenderer can only work properly on the main Cocos thread. Therefore, when I ran the game, it did not have any compilation issues but all of the loaded sprites were black and it did not fix the slow loading time. Is there another way to multithread that would not cause CCRenderer to crash and that would allow the level to be loaded while the player is playing it? Or perhaps I should not use multithreading and try to optimize this in a different way?

Ideas would be appreciated.

can you explain a bit more about the structure of your file?

t:2

is this like a tile id or some id that references what sprite should go here? If so, isn’t t: technically not needed. Just use 2?

x:4914
y:54

if these are (x,y) coordinates why not use CCPoint instead? CCPoint(4914, 54)

Your text file becomes shorter and you are parsing the points in one step instead of 2?

;

also, why not just put one per line and read one whole line at a time. Way more effiecient than reading in a text file char by char parsing at , and ;.

something like:

2, (4914,54)
2, (4915, 55)
2, (4916, 56)

I’m just making guesses based upon the picture. I could be way off until I know more.

t: is the texture of the sprite; it used to be the filename, but i changed it to be just a number that represents an id. you are right for x and y, those are the coordinates for the object;

the problem however does not lie in the parsing part, from what i have noticed it takes no time for my whole parse function to run and return a vector of a special class called LevelElement (it is a class for all objects in a level, and contains information about the objects color, position, texture, rotation, etc)

the problem lies within the rendering, aka making the actual sprite from the LevelElement object members properties. It is done all at once, therefore it pretty much pauses the thread for a good 1-2 seconds depending on the size of the level. What i wanna be able to do, is to draw all of these blocks while the player is going through the level, and I want to be able to restart the scene any time I want without major pauses for loading.

I just do not know how. Threading did not seem to work, but I know cocos2dx has a scheduler, can that scheduler be used to perform something such as loading a level asynchronously and partially while the level is being played?

Thanks for the tips for improving the file structure though; parsing is fast but it will not hurt if i try improving the file structure a bit more.

Well, I’d use threading and I’d get started making your objects from that vector as soon as you can. Perhaps while the user is waiting at your menu screen, pre-load the first level.

While they are playing the first level, load the second level when they have completed around 50%.

You can absolutely use std:thread to create objects you just should run them in the main cocos thread. I create lots of things in the background while the player plays my game. I use a vector<std::thread> for running my own timers for things to happen every few seconds to every few minutes, etc.

How would I create objects with std::thread so that the rendering does not mess up?

Also this game is a side scroller so there is a lot of dying involved, and I want to be able to completely reset the scene a lot of times.

I create all of my objects, populate their variables and stick them in vector, one for each level. I then iterate through the vector and .addChild() to where these objects need to be when I create a new Scene.

When the player dies, I just clear some variables, move my scene back to the beginning and the player starts again without any real delay.

Have you ever played Super Meat Boy?

1 Like

This seems pretty okay to me. And this is where you should show loading bar.
First show loading bar then start loading all objects, and once finished hide loading bar and start game.

1 Like

I realized that restarting the whole scene is inefficient by now. What I will do instead is just do kind of what you have told me: i am gonna move the player back to their original starting position, and do some minor cleanup so the level is completely reset without the scene needing to be reset as well.

I have never actually played super meat boy, but the reason why i was so persistent about wanting to restart the scene is because I am taking inspiration from games like The Impossible Game and the world’s hardest game. When you die in those, the level resets and you start from the beginning of the level.

Thanks for the advice.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.