Reloading function is nonfunctional

I have this function that returns 2 values:

  1. the clip value (usable bullets)
  2. the reserve value (reloadable bullets)

My implementation looks like this:

func reload(currentclip, maxclip, totalammo):
	var loadable = maxclip - currentclip
	return [loadable, totalammo - loadable]

It has no checks for negative values and had very little documentation on this, so I ask here for help. Thank you!

Hi,

If there’s no check for negative values, you can add it yourself.
Compute your reload quantity in a variable before returning it, so that you can perform some checks on it in between, like this:

var loadable = maxclip - currentclip
var reload_quantity = totalammo - loadable

if reload_quantity < 0:
    reload_quantity = 0:

return [loadable, reload_quantity]

I don’t know if that code will work perfectly, it’s just a way of sharing the idea. You can also use the min, max and clamp functions to clamp your final value.

Hey i am interesting to ask what do you want to do exactly with this?? Like something important or just want to subtract loaded bullets from the Total bullets?? And display reloadable bullets?? :sweat_smile::sweat_smile: