In some games you may want to preserve important values across many levels. Like hero who fought a difficult battle in level 3 should carry over his health and ammo to level 4. It’s a simple task to do with Mad Level Manager!

Explanation

Let’s say we want carry health and ammo. To make it working each level should define 2 properties when level is finished:

  • end_level_health
  • end_level_ammo

Now for the each level we will try to read these properties from the previous one at the start and make them current health and ammo values.

Implementation

Let’s say you have the Player.cs script. It has these two properties defined:

public int ammo;
public int health;

Now in Start() function we will look for previous level values. If there’s no previous level, the default values for ammo and health will be set.

void Start() {
	if (!MadLevel.HasPrevious(MadLevel.Type.Level)) {
		// this is first playable level, set the default values
		ammo = 50;
		health = 100;
	} else {
		// this is not first level, load values from the previous one
		var prevLevelName = MadLevel.GetPreviousLevelName(MadLevel.Type.Level);
		health = MadLevelProfile.GetLevelInteger(prevLevelName, "end_level_health");
		ammo = MadLevelProfile.GetLevelInteger(prevLevelName, "end_level_ammo");
	}
}

And it’s almost done! We only need one more function that should be called when player finishes the level. Of course this function should be called manually.

public void OnLevelFinished() {
	// store health and ammo values
	MadLevelProfile.SetLevelInteger(MadLevel.currentLevelName, "end_level_health", health);
	MadLevelProfile.SetLevelInteger(MadLevel.currentLevelName, "end_level_ammo", ammo);
}

Additional features

If you want, you can display your current ammo and health values on level icons! All you need to do is to add text properties to your icons and use end_level_health and end_level_ammo as keys to get the string value!