how to decrease the value of the progressbar automatically and slowly?

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

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)
:bust_in_silhouette: Reply From: i_love_godot

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.

    

thanks a lot u r amazing, this worked

Shlopynoobnoob | 2019-12-18 17:20