Implement reloading levels
created: 2025-03-14 22:10:23 UTClast updated: 2025-03-14 22:10:46 UTC
Today I decided it might be nice to be able to reload a level easily without having to exit a play session and re-enter it. This can come in handy as in the future I will not use the textures I currently have and want a way to sort of hot-reload level data on the fly. Also allows me to completely reset a level, at least so far. But of course, right now levels are simple. They have some player data, actor data and texture data.
When the game is started, a LoadData function is called at the beginning. This instantiate the player object and then runs the Loader against the level configuration file. When a game session is ended by either pressing escape or exiting the window, an UnloadData function is called that will simply loop over all the allocated data and destroy it properly.
When the game is started, a LoadData function is called at the beginning. This instantiate the player object and then runs the Loader against the level configuration file. When a game session is ended by either pressing escape or exiting the window, an UnloadData function is called that will simply loop over all the allocated data and destroy it properly.
void Game::LoadData() { player = new Player(this); Loader *loader = new Loader("assets/levels/new_test.yaml"); loader->Load(this); delete loader; } void Game::UnloadData() { while (!actors.empty()) { delete actors.back(); } delete map; }
As any good game loop, there is a process input step where keyboard input is collected. I am also working a little on mouse input, but that is not complete. Anyways, I already have a key designated that starts a sort of debug mode. This the is x key. The debug key right now will just render a mini-map in the top left corner showing the level, a player, and the rays cast by the player for what should be visible.
Since I already have a LoadData and UnloadData function, couldn't I just call those? Well, that is exactly what I have done.
if (key[SDL_SCANCODE_X]) { debug = !debug; } if (debug) { if (key[SDL_SCANCODE_R]) { UnloadData(); LoadData(); } }
Now I can press x and then press r to reload the level and reset it to what it originally was. This gives me the ability to add a new sprite without having to reload the entire application.