DEV Community

Cover image for Creating a proper experience system for your game
Pedro Fontoura
Pedro Fontoura

Posted on

Creating a proper experience system for your game

What is an experience system?

An experience or "XP" system is an engagement tool for your game that gives players a sense of progression through the gameplay.

This system is used very often in multiplayer games.

Why should I dedicate time to developing a proper experience system?

First, an experience system keeps the players playing your game. If you can not give enough attention while building this system, why should the player stick to your product after it is out there and the launch hype is out?

That is weird

Imagine that you are playing your game and just reached level 2. It took you roughly one hour to go up from level 1.

You continued playing, and now, you are a lot of hours deep into the gameplay, but different from the game start, you can not even see the next level far ahead. That would be very weird and highly demotivating, right? Exactly.

The XP progression of your game MUST make sense. Picking arbitrary values out of your mind to define the amount of experience per level is a no-no.

So, if the experience system needs to follow logic, what is the most logical thing in the world? Math.

1 + 1 = 2

There are infinite math functions that you could use to describe the experience progression curve.
I will not enter the subject of what function or equation is more appropriate or anything in that direction. Deciding which mathematical function to use is something you can discuss with the math guy on your development team.

You need to define a function that takes the level as a parameter and returns the amount of XP for that level, and vice-versa.

For this example, we will be using the following function to describe the curve of our experience system:

f(x) = x^2 + 3x

If we plot that function on Geogebra, we can visualize the shape of the curve:

Plot of the quadratic equation above

Let me see the code

The implementation of this system is pretty simple. There are essentially two base functions: getXPFromLevel and getLevelFromXP, everything else that is needed on a system like this will be built on top of them.

const INITIAL_FACTOR = 25;

const solveQuadraticEquation = (a: number, b: number, c: number): number[] => {
  const root = (-1 * b + Math.sqrt(Math.pow(b, 2) - 4 * a * c)) / (2 * a);
  const root2 = (-1 * b - Math.sqrt(Math.pow(b, 2) - 4 * a * c)) / (2 * a);

  return [root, root2];
};

const getXPFromLevel = (level: number): number => {
  if (level < 0) {
    return 0;
  }

  return (Math.pow(level, 2) + 3 * level) * INITIAL_FACTOR;
};

const getLevelFromXP = (xp: number): number => {
  const roots = solveQuadraticEquation(1, 3, (xp / INITIAL_FACTOR) * -1);

  const level = roots.find((root) => root >= 0);

  if (level === undefined) {
    return 0;
  }

  return Math.floor(level);
};
Enter fullscreen mode Exit fullscreen mode

There is not much to talk about the code because it is highly dependent on which math formula you choose.

A detail that I can point out is that I used an initial factor to adjust the experience values to be more aesthetic (e.g., the amount of XP needed to reach level 1 is 100 instead of 4.).

There is much more involved in an experience system, like level rewards and rewarding XP for interactions, but I will save those for another post. The idea here is to make a brief introduction to this subject.

Motivation

I wrote this post to showcase this way of building an experience system to a team with web development background and counterpoint a database approach that was getting strong support in a technical debate.

While creating a database entry for each level and storing the amount of XP needed to advance that level is doable, it's a far more resource-consuming and error-prone approach.

Educating and showing different perspectives is the best way to help your team make better decisions.

Top comments (1)

Collapse
 
puwhq_gsjzbwi_961848b6a3b profile image
Puwhq Gsjzbwi

The game provides an environment where players can experiment with different strategies without significant real-world consequences. By observing the outcomes of their decisions, players can refine their ability to assess risks for visit this website , learn from failures, and improve their judgment. This process of trial and error is valuable not only in games but also in various real-life situations, where taking measured risks and learning from past experiences are key to personal growth and success.