#Help with numbers

7 messages · Page 1 of 1 (latest)

dusty creek
#

the _process(delta) function gets called every frame, assuming your monitor supports 60 fps means that the game runs (f not lagging) at 60 fps, so _process(delta) gets called 60 times with delta being about 0.016

in your code, in each run of _process(delta) the GlobalVar.energy only gets increased by 1 at most - so as soon as you hit a higher value of GlobalVar.energypersec that exceeds your monitor framerate, then you only add one but drop all others

you must consider the case, that in one _process(delta) run, that you need to be able to add more than 1

like...

func _process(delta):
  if GlobalVar.energypersec > 0:
    timer += delta
    var energy_to_add = timer * GlobalVar.energypersec
    if energy_to_add > 1.0:
      GlobalVar.energy += floor(energy_to_add)
      timer -= floor(energy_to_add) / GlobalVar.energypersec

mind that cutting the timer to 0 means losing precision, as you should only remove so much from the timer that was added as energy

it would be probably better to store the energy as a float instead of a int, so you do not have to consider discrete increment steps

func _process(delta: float) -> void:
  if GlobalVar.energypersec > 0.0:
    GlobalVar.energy += delta * GlobalVar.energypersec
mental timber
dusty creek
#

floor is a math function that floors the value to the next lower whole number

floor(5.5) = 5
floor(3.1115442) = 3
floor(-1.899) = -2
floor(2) = 2

https://docs.godotengine.org/en/stable/classes/class_%40globalscope.html#class-globalscope-method-floor

the float in delta: float is a type hint, an optional mark to make sure the variable delta is always a float and can't be something different

the -> void: after a function is also a type hint, indicating that this function should not return anything

https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/static_typing.html

shy turret
#

so its rounding

dusty creek
#

floor always rounds down
ceil always rounds up
round rounds to the nearest whole number

shy turret
#

so floor (5.7) would still be 5 ?

dusty creek
#

exactly