Help with level system

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

(Sorry for my english i’m using google translator)
I had a problem that when I add +1, for some reason I get +76

extends ProgressBar

var exp_bar = 0
var level = 0

func _ready():
    $Level.text = str(0)

func _input(event):
  if event.is_action_pressed('level'):
	   exp_bar += 50

func _physics_process(delta):
    get_node('.').set_value(exp_bar)
    level += 1
    if exp_bar >= 100:
	    $Level.text = str(level)
	    exp_bar = 0
:bust_in_silhouette: Reply From: Magso

You’re adding 1 every physics frame. You could put everything in _physics_process into the if statement in _input. If you need it to be in _physics_process you’d need to check if something is true to stop a loop, for example using a bool.

var  add_to_level : bool

func _input(event):
  if event.is_action_pressed('level'):
       exp_bar += 50
       add_to_level = true

func _process(delta):
    if add_to_level:
        get_node('.').set_value(exp_bar)
        level += 1
        if exp_bar >= 100:
            $Level.text = str(level)
            exp_bar = 0
        add_to_level = false

Everything works, but seravno adds +2, not +1

Comet | 2019-07-05 14:01

change func _physics_process(delta): to func _process(delta):so you’re not using physics frame rate. I’ve corrected it above.

Magso | 2019-07-05 14:08