Add certain points per level up

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

Making a stat system and i wanna make it so it add a certain points to all stats and not randomly pick one as all other tutorials show it.

example 1-9 add 10 points to one stat and 5 to the others and increase it slightly 10 -19 and so on.

Sorry for the bad grammar!

And this is how the code looks like.

  extends Node
 # Character stats
export (int) var max_hp = 10
export (int) var max_mana = 10
export (int) var strength = 1
export (int) var dexterity = 1
export (int) var agility = 1
export (int) var magic = 1
export (int) var defence = 1
export (int) var speed = 1
export (int) var wisdom = 1
 # Leveling 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(pow(level, 1.6) + level * 4)
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)

Edit: Sorry for the not so good explaining
level 2 add 10 to all var exept maxhp and maxmana
add 5 maxhp and maxmana

level 3 same as level 2 all the way to level 9
and from level 10 add 15 to all var exept maxhp and maxmana
and repete that type of pattern all the way until max level that is not decided yet

hops this explains it better

Not sure what are you asking. If you want to add predetermined values to specific stats, just do it. Like so:

magic += 2
speed +=1
max_hp += 14
etc...

Since I guess this isnt what you are asking for, try to be more specific with what you want to achieve (since I cant understand the example you gave, 1-9 what?, do you want the stat that gets 10 to be picked at random and give 5 to all others? even the max_hp ones? etc…)

Pomelo | 2022-07-25 19:15

Sorry for the not so good explaining
level 2 add 10 to all var exept max_hp and max_mana
add 5 max_hp and max_mana

level 3 same as level 2 all the way to level 9
and from level 10 add 15 to all var exept max_hp and max_mana
and repete that type of pattern all the way until max level that is not decided yet

hops this explains it better

FatherCCP | 2022-07-25 19:28

:bust_in_silhouette: Reply From: jgodfrey

While there are likely more concise ways to do this, without more specifics on the value increases through level progressions, it’s hard to say. A simple way to accomplish this would be to just brute-force it. So, something like:

func update_stats(level):
	if level >= 2 && level <= 9:
		max_hp += 5
		max_mana += 5
		strength += 10
		dexterity += 10
		# ...
	elif level >= 10 && level <= 20
		max_hp += 5
		max_mana += 5
		strength += 15
		dexterity += 15
		# ...

Then, at the point where you advance your level, just call update_stats() and pass in the new level. That function will than make the necessary stat adjustments.

Again, there are likely better ways to accomplish this, but without understanding exactly how each value should progress throughout the game, it’s hard to provide more info…

:bust_in_silhouette: Reply From: Inces

There are 2 ways of doing this.
1.
Introducing stats grouped in a dictionaries, and iterating them on level up :

    export var stats = {"agility" : 1,"magic" : 3, "speed" : 4}
    export var otherstats = {"maxhp" : 10, "maxmana" : 15}
    for key in stats.keys() :
          if level < 10:
                stats[key] += 15
          else:
                stats[key] +=10
   for key in otherstats.keys():
          thesame

Leaving stats like You introduced them and designing temporary indicator arrays

for stat in ["magic","speed","agility"] :
        if level < 10:
               set(stat,get(stat) + 15)
        esle:
               set(stat,get(stat)+10)
for stat in ["maxmana","maxhp"] :
        thesame