Code will not get correct value

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

My code is suppossed to have a progressbar that will show you how full your storage is, but instead it says that the bits / capacity * 100 is 0, and only changes it to 100% once its at max, and i have no idea why. The code in question:

func _process(delta):
    $Label.text = str(Bits)
    Second += delta
    if Bits > 0:
        $ProgressBar.value = round(Bits / Capacity) * 100
        print(round(Bits / Capacity) * 100)
    if Second > 1:
        Second = 0.001
        if Bits < Capacity and Bits + Spark_Gens < Capacity:
            Bits = Bits + Spark_Gens
        if Spark_Gens > 0:
        Bits = Capacity
:bust_in_silhouette: Reply From: jgodfrey

I assume that both Bits and Capacity are integer values, right? If that’s the case, you’ll only get an integer value as the result of that division - which isn’t what you want. To ensure that you get floating point math, at least one of the values must be a float. A simple change to make it would would be:

round(float(Bits) / Capacity) * 100

Notice that I’ve forced the Bits value to be a float.

This is typical of many programming languages, including GDScript.

That doesnt work, now its stuck on 0%.

MrBucket | 2022-08-14 02:16

I’m pretty sure this IS your problem - though, maybe you have a similar issue elsewhere too. You definitely want float math, and you’ll only get that if one of the values is, itself, a float. Also, not sure why you need the round call.

Happy to take a look if the project is available for review.

jgodfrey | 2022-08-14 02:45