Magazines

Magazine (Abstract Class)

The Magazine class is an abstract base for implementing various ammo systems such as bullet clips, rechargeable energy units, or custom behavior. It defines the interface and default logic expected from a weapon magazine.

Summary

This component:

  • Tracks clip size and depletion
  • Triggers reload and depletion events
  • Is intended to be extended by subclasses (e.g., BulletMagazine, RechargeableMagazine)

Fields

FieldTypeDescription
_clipfloatCurrent number of bullets or units in the clip.
_clipSizefloatMaximum allowed bullets or units.
weaponWeaponThe weapon using this magazine. Set by the weapon when the magazine is assigned to it.
events.OnDepletedUnityEventCalled when the clip reaches zero.
events.OnStartReloadUnityEventCalled when a reload begins.
events.OnReloadCompleteUnityEventCalled when a reload finishes.

Public Methods

MethodDescription
virtual void Reload()Override to implement custom reload logic. Called by Weapon.Reload().
float GetClip()Returns the current clip value.
float GetClipSize()Returns the clip size.
virtual bool HasAmmo(float amount)True if the clip holds at least amount. No side effects.
void ConsumeClip(float amount)Reduces the clip, never below zero. When it runs out, raises OnDepleted and stops the weapon.
virtual bool IsDepleted()Checks if the clip is empty. If so, triggers OnDepleted (at most once per frame).
virtual bool KeepWeaponShootingWhenEmptyWhen true, running empty doesn’t stop the weapon; it fires again as soon as ammo is available.

Inheritance Notes

To implement a custom magazine:

public class InstantMagazine : Magazine
{
    public override void Reload()
    {
        events.OnStartReload.Invoke();
        _clip = _clipSize;
        events.OnReloadComplete.Invoke();
    }
}

Tip

Use UnityEvents like OnDepleted or OnReloadComplete to hook animations or sound effects.

Note

A weapon with no magazine has infinite ammo, so magazine code never runs for it.

See Also

Weapon

Barrel

BulletMagazine

RechargeableMagazine