'); -->

# Carry values across levels

In some games you may want to preserve important values across many levels. Taking as an example a hero who fought a difficult battle in level 3 and 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 to carry health and ammo. To make it working each level should define 2 properties when the level is finished:

- end_level_health
- end_level_ammo

Now for 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 the 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 a 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 the 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!