0 votes

I posted on reddit and I want to post here too so I am trying to make the node Progressbar to decrease value automatically but it goes from 100% to 1% immediately when I run the scene and I want it to decrease slowly. this is my code:

func _process(delta): 
    for n in range(100, 0, -1): 
        get_node("/root/Node2D/bar").value=int(n)
asked Dec 18, 2019 in Projects by Shlopynoobnoob (45 points)
reshown Dec 18, 2019 by Shlopynoobnoob

1 Answer

+1 vote
Best answer

That for loop is being run every frame and by the end of each frame the value is 1.

First I would set a constant to control how quickly the bar progresses. For the example below I've chosen to decrease by 5 per second so I multiply that by delta to keep the change consistent:

const BAR_SPEED = 5
var current_bar_value = 100


func _process(delta):
    current_bar_value -= BARSPEED * delta

    # Don't go below zero
    current_bar_value = max(current_bar_value, 0)

    # Assuming this is a node you have set up
    get_node("/root/Node2D/bar").value = current_bar_value

You could also setup a timer and update the bar at regular intervals that way, but the method above will probably be smoother. Hope this helps.

answered Dec 18, 2019 by i_love_godot (352 points)
selected Dec 18, 2019 by Shlopynoobnoob

thanks a lot u r amazing, this worked

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.