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
| Field | Type | Description |
|---|---|---|
_clip | float | Current number of bullets or units in the clip. |
_clipSize | float | Maximum allowed bullets or units. |
weapon | Weapon | The weapon using this magazine. Set by the weapon when the magazine is assigned to it. |
events.OnDepleted | UnityEvent | Called when the clip reaches zero. |
events.OnStartReload | UnityEvent | Called when a reload begins. |
events.OnReloadComplete | UnityEvent | Called when a reload finishes. |
Public Methods
| Method | Description |
|---|---|
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 KeepWeaponShootingWhenEmpty | When 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.