Level Reloading
Restarting Levels with F5
Since the Lab 10 level has pits you can fall through, we need an easy way to reload the level in case you get stuck. Eventually, we also will need a way to load into any arbitrary level. So we need something flexible enough to handle that.
The basic idea is this:
- Have a
std::string
inGame
that contains the name of the next level we want to load, but it starts out empty - At the end of
Game::UpdateGame
we check to see if the string is set to something (meaning a level load has been requested). If it is, we unload the current level, load the next level, and set the string empty again
The steps for unloading the current level and loading in the new level are:
- Call
UnloadData()
- Clear your collider vector and/or any data structure you’re using to store the door information (like a map), just in case the destructors for your actors don’t properly remove
- Call
StopPlayback()
on the input replay pointer - Call
StopAllSounds()
on the audio system - Set the blue portal and orange portal pointers to null
- Set the current level string to the next level string
- Call
LevelLoader::Load
on the new level name - Clear the “next level” string
Setup the code for this. Then, in Game::ProcessInput
set it up so that on a leading edge of SDL_SCANCODE_F5
, you set the next level string to the current level name. This will force a reload to occur.
For some reason, on Macs you may need to press F5 twice for it to register.
Put a breakpoint on the LevelLoader::Load
line in your code that loads the next level. Load into the game and move around a little bit. Hit the F5 key in game, which should cause your breakpoint to get hit. Before you hit continue, confirm that your actors and colliders vectors in game are empty. If they aren’t, it likely means either somewhere you’re double-adding something or you forgot to remove a collider in a destructor.
After you hit continue, confirm that the level reloads as expected. Then, remove the breakpoint, and try a few more times to verify the loading works correctly.
Reloading When Falling Too Far
In PlayerMove::Update
, add code that says if the player’s z-position is less than or equal to -750, reload the level.
Confirm that when you jump into the pit, the level reloads, like this:
Testing the Input Replay
Finally, confirm that you can pass the input replay validation mode. Remember that you must pass replay validation for an A on this lab (though not for a B). For best results, you should start the replay as soon as you load into the game.
If your replay works, it should match this video:
Once you’ve pushed your code, you should review the grading specifications to confirm you’ve satisfied them.