#Delta inaccurate

1 messages · Page 1 of 1 (latest)

surreal bluff
#

Hi, I have made a simple sprint system but I have a problem with the energy replenish and deplate time.

gv.energy_deplation_rate and gv.energy_replenish_rate are both set to 100

I made a progress bar for it and its default and max. value is 1000. In the code it should deplate the energy in 10 seconds and replenish it also in 10 seconds.

Problem is that it will deplate in around 9 seconds and replenish in about 16 seconds...

Here is the code:

    # Add the gravity.
    if not is_on_floor():
        velocity += get_gravity() * delta
    var direction := Input.get_axis("move_left", "move_right")
    var is_sprinting = false
    # --- SPRINT ---
    var sprint_multiplier = 1
    if Input.is_action_pressed("sprint") and gv.energy > 0 and direction != 0:
        is_sprinting = true
        gv.energy -= gv.energy_deplation_rate * delta
        sprint_multiplier = gv.sprint_bonus
    else:
        sprint_multiplier = 1
        is_sprinting = false
    
    if not is_sprinting and gv.energy < 1000 and is_on_floor():
        gv.energy += gv.energy_replenish_rate * delta```
modest harness
#

First off, it's "depletion" not "deplation", same for "deplate" -> "deplete"
Second, could it be that the type of gv.energy_deplation_rate and the other is integer?
if you cast it to float when adding or removing delta, does it help?
gv.energy -= float(gv.energy_deplation_rate) * delta

#

I'd question the type of gv.energy too.
Doing the above calculation may result in decimals, so if gv.energy is an integer it may just round up or down the value.