How to Employ a Levelling System into Game

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By phinqs

I am trying to employ a levelling system into my game. So far, I can grant experience for certain activities in game, retrieve the remaining experience required to level up, and level up a skill if that experience is achieved/surpassed.

As you can see within my level_up() function, I have attempted to use arrays to separate different levels (with the aim of adding more at a later date). I am currently manually adding experience to “atk” using the array index skillStat[0] but I would like to create a better way to manage and manipulate this data.

For example, when carrying out actions in-game I would like to grant experience, x, in skill, y. Currently, I am specifying how much experience to grant but not in the relevant skill. Ofcourse, I can make multiple variables and functions but I know there is a better way to achieve this.

Is arrays the best way to do this? If so, can anyone offer some guidance/advise/solutions?

# CHARACTER LEVELS
export(int) var atk = 1
export(int) var def = 1

# LEVELLING SYSTEM
export(int) var level = 1
var experience = 0
var experience_total = 0
var experience_required = get_required_experience(level + 1)

func get_required_experience(level):
	return round(75 * pow(1.1, level))

func gain_experience(amount):
	experience_total += amount
	experience += amount
	while experience >= experience_required:
		experience -= experience_required
		level_up()

func level_up():
	level += 1
	experience_required = get_required_experience(level + 1)

	var skillStats = ["atk", "def"]
	var skillStat = skillStats[0]
	set(skillStat, get(skillStat) + 1)
:bust_in_silhouette: Reply From: Inces

This calls for dictionaries.

var stats = {"strenght : 10, "dexterity" : 6,"vitality":10 } 
for s in stats.keys() :
      stats[s] += 1

Seems like this sollution will be shortest and ellegant and will allow You to keep all data about stats in this one dictionary, instead of having multiple individual variables.

This appears to be the kind of thing I was looking for. Although, does that mean I should make multiple dictionaries?

For example:

var stats = {"strength" : 10, "dexterity" : 6, "vitality" : 10}
var stats_xp = {"strength_xp" : 100, "dexterity_xp" : 200, ...}

I will investigate dictionaries further.

phinqs | 2022-02-03 17:59

If You are going for Elder Scrolls kind if levelling, where every stat has different exp values, I would recommend doing nested dictionary like:

var stats = {"atk: {"value": 10, "currentexp" :0, "currentlvl" : 1,"maxvalue":100},{"def":{ 

But in many cases it would be better to make multiple dictionaries instead of one nested. It all depends how often do You want to do an operation on all statistics and their properties. For example if single trigger of levelup() function makes you check or calculate both stats_xp and stats itself and some other staff regarding stats : than You want to have massive dictionary, because it is easier to iterate through single dictionary.

However if You choose to have multiple dictionaries, important thing is to have exact names of keys in all dictionaries ! :

var stats = {"strength" : 10, "dexterity" : 6, "vitality" : 10}
var stats_xp = {"strength" : 100, "dexterity" : 200, ...}

this way You can iterate all of your dictionaries in one go like this :

for key in ["strength","dexterity","vitality"]
         stats[key] += 10
         stats_xp[key] += 100

Inces | 2022-02-03 20:51