Gun reload logic problem


#1

I am making a topdown shooter and have some logic problems with gun reload.

I did some vars to allow me to reload.

Pistol have vars to: max ammo, current ammo and reload need to max ammo.
Player have var ammo amount.

THE LOGIC IS:

I. Player ammo amount > reload ammo need = player have more, so max ammo of pistol and subtract the ammo reload need from player.

II. Player ammo amount = reload ammo need = player have exacly ammo, so just make pistol max ammo amount (15) and makes player ammo = 0

III. Player ammo amount < reload ammo need = player have less ammo than need, so do pistol current ammo + player ammo amount, and after that makes player ammo = 0

Works fine in almost all cases, but have some problems:

  1. when I have 5 bullets in Pistol and 15 on player, the game reload all making the pistol have 20 and player 0.
  2. when I have 10 bullets in Pistol and 10 on player, the game math do pistol have 15 and player 0 (losting 5 in the process)

Seems the gdev is using the wrong logic:

Because in the case 1, the engine should use the logic I, but seems the engine is choosing the logic III.

In the case 2, the engine should use the logic I, but seems the engine is choosing the logic II.

Idk where I am wrong :confused:


#2

Not sure where it is wrong either, but I suggest you try “While” type event, to increment the bullet one by one.
Here is an example:


In that picture my while event uses Temp.SlotNum as index of loop variable, and the loop will be over when it is above Inventory.MaxSlot.
With something like this, you can check if pistol ammo > max pistol capacity, then if the condition is true add actions to subtract 1 from player ammo, and add 1 to pistol ammo.
By the way if your While does not end your game will stop rendering so be careful about that.