0 votes

(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
asked Jul 5, 2019 in Engine by Comet (60 points)

1 Answer

0 votes
Best answer

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
answered Jul 5, 2019 by Magso (2,867 points)
edited Jul 5, 2019 by Magso

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

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.