RE Weapon System II: Modify Reloading

Hi again,
I hate to bug you guys with this stuff because i know it's a silly detail that I'm missing è_é

So basically I have this ABS scritp that has weapons (firearms) and item that works as ammunitions (example: weapon 1 requires item 5, and so on...). What it does is, when the weapon is empty, you hit S and the game consumes 1 ammunition items and reload the weapon to the max capacity. What I wanted instead is:
1 - Reload the gun at any time, even if you don't have 0 ammo;
2 - Treat every "ammuniton item" as a single bullet (meaning that, for example, if my gun holds up to 15 bullets and i currently have 11, the game - upon reloading - will remove 4 ammos from my inventory and reload my gun to 15;

The first issue was easy to solve, all I did was, change this:

Code:
if Input.trigger?(Reload_Key)
        if @equipped_weapon.bullets == 0
          reload(@equipped_weapon)
        else
          Audio.se_play("Audio/Se/Buzzer1", 80, 100)
        end
      end

into this:

Code:
if Input.trigger?(Reload_Key)
        reload(@equipped_weapon)

even tho, It just occurred me that I should put some "if" condition, in case the player hits the reload button wile the weapon is full... something along the line of:

Code:
if Input.trigger?(Reload_Key)
        if @equipped_weapon.bullets < @equipped_weapon.max
          reload(@equipped_weapon)
        else
          Audio.se_play("Audio/Se/Buzzer1", 80, 100)
        end
      end
would be correct?

Second issue is what's bugging me, as I had to write myself some way to calculate how many bullets it has to reload everytime,
so I went ahead and change this:

Code:
 def reload(weapon)
    $game_party.items.each { |item|
      if item.id == weapon.magazine.id
        $game_party.lose_item(weapon.magazine,1)
        @equipped_weapon.bullets += @equipped_weapon.max
        Audio.se_play("Audio/Se/#{weapon.wav_name}_reload", 100, 100)
        @recover = @equipped_weapon.recover
        return
      end
    }
    Audio.se_play("Audio/Se/Buzzer1", 85, 100)
    @recover = (@equipped_weapon.recover > 40 ? @equipped_weapon.recover : 45)
  end

into this:

Code:
 def reload(weapon)  #MODIFIED BY OTTO TO TREAT EACH "AMMO ITEM" AS A SINGLE BULLET
    $game_party.items.each { |item|
      if item.template_id == weapon.magazine.id
        ammo_id = weapon.magazine.id
        ammunition = $game_party.item(weapon.magazine.class)[ammo_id]
        max_ammo = @equipped_weapon.max
        current_ammo = @equipped_weapon.bullets
        ammo_reloaded = ( max_ammo - current_ammo > ammunition ? ammunition : max_ammo - current_ammo )
        $game_party.lose_item(weapon.magazine, ammo_reloaded)
        @equipped_weapon.bullets += ammo_reloaded
        Audio.se_play("Audio/Se/#{weapon.wav_name}_reload", 100, 100)
        @recover = @equipped_weapon.recover
        return
      end
    }
    Audio.se_play("Audio/Se/Buzzer1", 85, 100)
    @recover = (@equipped_weapon.recover > 40 ? @equipped_weapon.recover : 45)
  end

I'm sure I'm on the right path but I get a crash as soon as I reload, that seems to be tied to this strig here:
ammunition = $game_party.item(weapon.magazine.class)[ammo_id]

Any advice?
🙂
Code:
 def reload(weapon)
    $game_party.items.each { |item|
      if item.id == weapon.magazine.id
        $game_party.lose_item(weapon.magazine,1)
        @equipped_weapon.bullets += @equipped_weapon.max
        Audio.se_play("Audio/Se/#{weapon.wav_name}_reload", 100, 100)
        @recover = @equipped_weapon.recover
        return
      end
    }
    Audio.se_play("Audio/Se/Buzzer1", 85, 100)
    @recover = (@equipped_weapon.recover > 40 ? @equipped_weapon.recover : 45)
  end
I would have maybe just put in one extra line
Code:
 def reload(weapon)
    $game_party.items.each { |item|
      if item.id == weapon.magazine.id
        $game_party.lose_item(weapon.magazine,1)
        ammo_reload = @equipped_weapon.max -= @equipped_weapon.bullets
        @equipped_weapon.bullets += ammo_reload
        Audio.se_play("Audio/Se/#{weapon.wav_name}_reload", 100, 100)
        @recover = @equipped_weapon.recover
        return
      end
    }
    Audio.se_play("Audio/Se/Buzzer1", 85, 100)
    @recover = (@equipped_weapon.recover > 40 ? @equipped_weapon.recover : 45)
  end
@Roninator2 that didn't solve the problem of 1 item = 1 bullet unfortunately 🙁

anyway, I did it at last, apparently I had to put
ammunition = $game_party.item_container(weapon.magazine.class)[ammo_id] in that line indicated by the crash error.
Still not sure what that means, but at least it works lol

Original thread: https://forums.rpgmakerweb.com/threads/re-weapon-system-ii-modify-reloading.99064/