Smart way of loading the next level


So, you have a mode in your game where after you finish a level, it throws you into the next one. I don't know if anyone else ever struggled with this, but i first had it so that you had to each time write a piece of code that changed the variable that stores the next loadable levels address. Also i had to set the maximium amount of levels by hand. Like "if (progress == 3) {nextlevel = "levels/level3/";}". One of these for every level- dumb. Instead i made it like this "if (progress >= 1) {nextlevel = "level" + to_string(progress) + "/";}".Then, to the first thing that is loaded onto a level, i made the code so that if the file cannot be read, which means that there is no file like that in the address the code set, the game goes back to the level menu. The progress variable is always increased as the hole is reached, but since the "nextlevel" is also used in checking and saving highscore, i had to make the previous code come after the highscore has been checked, at the beginning of the first loading part.

It feels like an obvious way to do it now, but at first i just thought of "quick and simple" way to make the stuff work. Work smarter, not harder, is definetly true in coding- yet here i am making games just using c++ and sdl2. IT'S FUN, for me atleast, and allows me to easily port the game to raspberry pi, which i have two of.

So. Thats a quick tutorial on how i made it so that you can add as many levels as you want to be played one after another.

Sorry if the english is a bit odd. Im form Finland, so, english is not my first language.

Files

NTS_GOLF_V0.3_LINUX64bit.zip 4 MB
Nov 19, 2022

Get NotThatSad Golf

Leave a comment

Log in with itch.io to leave a comment.