DevLog: 33 Leveling System using Properties

PieceofPhie
4 min readOct 12, 2022

--

I recently added a leveling system to reward the player for slaying enemies. Slaying more enemies will make the player stronger and allow me to implement new challenging enemies.

Scripting

The main Ingredient to the leveling system is the use of properties. I wrote a article in the past on properties with a mock leveling system within it here:

https://vixian.medium.com/c-properties-in-unity-e5188dcc719f

This system is building off of that article. A quick rehash of a propery

public int LevelUp
{

get
{
return CurrentLevel;
}
set
{

_experienceBar.maxValue = ExperienceToLevel;

}
}

When we pass a variable to this property, everything nested in , ‘Set’ will execute, Vice versa to ,’ Get ‘.

The Code

I have my player script handling leveling and experience since the variables that are needed to increase per level are easily accessible. Slain enemies have a ,’experience to yield’ variable that will be passed to a public experience variable within my player script.

The system is split into 2 properties. One that’ll handle the amount of experience the player has and the other handling leveling up.

Experience Property

They’re 4 variables that plug into and handle all of this. Additionally I have UI Elements that display experience and leveling, but I wont be covering that in this article.

experience is where all gained experience will be stored into

ExperienceToLevel is the amount of experience the player needs to level up and _experienceScaler increases the amount of experience needed to level per level.

We insure that all code goes under this so that all variables are working with updated values.

When the player reaches the appropriate amount of experience, this If statement will execute. Which will then process the players amount of experience appropriately.

I have conditional statement for when the player reaches the EXACT amount of EXP and a loop for when the player has more than the exact amount.

Exact Amount

Straightforward. We add 1 to the ,’ LevelUp ‘ property and zero everything out so that the player can begin building up to the next level again.

Over Level

While the player as more experience than needed, we add subtract the amount of experience to level with the current experience to get the remainder of experience.

Mathf.abs or getting the absolute value is there because there was a consistent situation where the number would return negative.

I add 1 to the Levelup property and created an additional precaution to check if the number is not negative before updating all experience related variables.

Final update to the players experience

Since the over leveling loop only updated the players values when the experience if greater, I made a conditional statement that’ll update all related variables at the end of the process.

LevelUp Property

Whenever we add 1 to the LevelUp property, we display UI to show the player has leveled up, heal the player to max health, increase max health/damage, update the UI, and increase the amount of experience needed to level.

Notification

This communicates to my textmesh text scripts which notifies the player that they have leveled up.

Increasing Stats

This is hard coded and static for now. I may have a feature in the future that’ll allow the player to increase a specific stat such as movement speed, damage, or health by pressing 1, 2 , or 3.

UI Update

We ensure that everything is up to date on the players UI.

Increasing the amount of EXP to level

Finally, we simply add the _experienceScaler to ExperienceToLevel to increase the amount needed to level. _experienceScaler is adjusted via inspector.

_experienceBar is a UI element that represents the amount of experience the player currently has. In this case, we are increasing its max value to accurately display the players progression

Result

--

--