Weapon System
whip_gunny is WHIP's weapon system: guns, magazines, ammunition, melee weapons, throwables and the jerry can. It ships with WHIP and needs no setup: its items are in /whip → Economy → Items, ready to sell or hand out.
A resource that calls it adds dependency 'whip_gunny'. For gunny as a worked example, see Learning from gunny.
How it works
Weapons are inventory items with state, kept in whip_core's item metadata: gunny owns the rules, whip_core owns the data. A revolver or pump shotgun carries its rounds itself.
gun item ─ metadata ─┬─ serial "AB12CD34EF56"
├─ attachments { muzzle = 'gunny_suppressor' }
├─ attachmentMeta { muzzle = { ... } }
└─ magazine ─┬─ item 'gunny_mag_rifle'
├─ serial 'GH78IJ90KL12'
├─ ammo 24
└─ ... the rest of the magazine's metadata (its uid)- Drawing. The server records where the gun was drawn from and its serial; shots, throws and holstering go by that record. When the gun leaves its slot (dragged, stashed, handed over), the server puts it away.
- Shooting. The game reports the rounds each frame fires and the server takes them from the metadata. The game's own count is believed only when it's lower.
- Reloading. R swaps in the fullest magazine carried, or loads loose rounds into a gun with no magazine. Firing stops until the reload is done, and the gun can't be put away or swapped meanwhile (Finish reloading first).
- Loading magazines. Load Bullets runs a bar on the server's clock, 300 ms a round, not in water or while falling (
notWhile). Unload puts the rounds back in the pockets.
Magazines and calibers
- Magazine-fed guns take separate magazine items, several kinds per gun.
- No magazine (
magless): revolvers, tube-fed and break-action shotguns, launchers. R loads loose rounds at once, or a shell at a time for the pump, pump Mk II, sawed-off and combat shotguns (loopedReload), stopped by firing or running. - Calibers. Each is an ammo item:
'9mm'is'ammo_9mm', loose rounds. A magazine takes one caliber, and item cards show it as Ammo Type.
Controls
| Key | Controller | Action |
|---|---|---|
| R | B | Reload, on foot only |
| G | LB + B | Check the ammo; with sticky bombs out, set them all off |
| E at the bench | A | Use the weapon bench |
Players can rebind R and G, and move the controller buttons in the pause menu. In a seat, reloading is automatic (the last round, or the trigger on an empty gun) and takes at least the weapon's reloadTime. On a controller, double-tap RB to start or stop aiming, then hold it to fire.
The magazine slot, throwables and the jerry can
- The magazine slot. Open on a gun shows its magazine, or its rounds, as one slot to drag in and out of. It's a panel item (
server/magpanel.lua, the panel guide's worked example). - Throwables: grenades, sticky bombs, mines, pipe bombs, molotovs, BZ and tear gas, flares, snowballs, acid packages and newspapers. Each is a stack held whole, and each throw comes off it. The acid package and newspaper need a recent game build.
- The jerry can (
jerry_can): its ammunition is its fuel (metadatafuel, 0-100%), poured on the ground and lit. Pouring it into a car and filling it at a pump are whip_fuel's, throughfuelCapacity. A new can is empty.
The weapon benchServer owners#
Attachments go on and come off at the weapon bench: E at it with a gun in hand. Each attachment is an item, and each weapon lists what it takes slot by slot (scope, muzzle, barrel, grip, flashlight) as GTA components in items/attachments.lua. The bench's place, model and label are Config.WeaponBench in gunny's config.lua; enabled = false removes it.
Adding weaponsServer owners#
- One entry per item in
items/weapons.lua,magazines.lua,calibers.luaorattachments.lua, each headed by every field and its default. Thenrestart whip_gunny. - A gun needs
hash(GTA's name in backticks, listed in whip_core'sshared/gta_weapons.lua), acaliberfromitems/calibers.lua, andcompatibleMags, ormagless = truewithinternalCapacity. Itspropmodel is inshared/gta_weapon_models.lua.loopedReload = trueneeds looped reload animations in its weapon meta. - A melee weapon needs only
weight,description,hash,label,propandmelee = true. Icons arehtml/img/items/<key>.webp(or.png). - A broken entry is left out, with a
[GUNNY] WARNINGconsole line saying why. - Sizes and reload times can be changed per item in /whip → Economy → Items, which wins over the file until you Reset.
To add guns without editing gunny's files, so updates never touch them, use a weapon pack.
From your resource
A gun is an ordinary item: AddItem gives one empty, RemoveItem takes one, and GiveWeapon adds a magazine, rounds or your own serial. The held gun is GetHeldItem(source) on the server and GetDrawnWeapon on the client.
Give a gun, and take that one back#
For an armory or a mission's kit, note the serials you hand out and take back those. FindItemByMetadata finds each, and StoreTake takes it (a drawn gun is put away first, its magazine with it). A spare magazine put into a gun is inside it, so its serial finds nothing until it comes out.
local issued = {} -- [source] = the serials handed out
local function issue(source)
local ok, where, gunSerial = exports.whip_gunny:GiveWeapon(source, 'gunny_carbinerifle', { loaded = true }, 'armory issue')
if not ok then return false, where end
local _, _, magSerial = exports.whip_gunny:GiveMagazine(source, 'gunny_mag_carbinerifle', 999, 'armory issue')
issued[source] = { gunSerial, magSerial }
return true
end
local function takeBack(source) -- exactly what was issued
for _, serial in ipairs(issued[source] or {}) do
local at = exports.whip_core:FindItemByMetadata('serial', serial)
if at and at.kind == 'player' and at.id == source then
exports.whip_core:StoreTake(at, 1, { reason = 'returned to the armory' })
end
end
issued[source] = nil
endEvery shot fired#
whip_gunny:weapon:fired comes each time the server takes rounds off a gun: automatic fire is counted a frame at a time, so shots can be more than 1. It runs as often as anyone fires, so keep your handler short.
-- Server: a "shots fired" call, at most one a minute per shooter
local lastAlert = {}
AddEventHandler('whip_gunny:weapon:fired', function(e)
local now = GetGameTimer()
if e.coords and now - (lastAlert[e.source] or 0) > 60000 then -- coords is nil once they've gone
lastAlert[e.source] = now
AlertPolice(e.coords, e.item) -- your own
end
end)The rounds on your HUD#
The client has no per-shot event: read GetDrawnWeapon a few times a second between whip_gunny:weapon:drawn and :holstered. Its count is live.
local following = 0 -- each draw and holster bumps it, ending the loop before
AddEventHandler('whip_gunny:weapon:drawn', function(w)
following = following + 1
local mine = following
if w.kind ~= 'gun' then return end
CreateThread(function()
local gun = exports.whip_gunny:GetDrawnWeapon()
while following == mine and gun do
ShowAmmo(gun.ammo, gun.capacity) -- your own
Wait(150)
gun = exports.whip_gunny:GetDrawnWeapon()
end
end)
end)
AddEventHandler('whip_gunny:weapon:holstered', function() following = following + 1; HideAmmo() end)No weapons here
For a no-weapons zone, handcuffs or jail, call BlockHands with only = 'weapons', on the client for a place or the server for one player. gunny refuses a draw with the block's label and puts away a drawn gun until UnblockHands.
exports.whip_core:BlockHands(source, 'myjail', { only = 'weapons', label = 'No weapons in here' })A zone's own Weapons away rule works on GTA's weapons, not gunny's.
Weapon packs#
A weapon pack adds guns from your own resource without editing gunny. It streams the weapons, ships icons as html/img/items/<item>.webp, and registers each item inside Gunny.register (no dependency needed), magazines first, in a file both sides load. A pack's gun needs what any gun needs (Adding weapons), may use gunny's magazines and calibers, and gets gunny's menus, magazine slot, settings and bench.
-- fxmanifest.lua (models and textures go in stream/)
shared_script '@whip_gunny/lib/pack.lua'
shared_script 'gunny.lua'
data_file 'WEAPONCOMPONENTSINFO_FILE' 'data/mypack_weaponcomponents.meta' -- before the weapons that list them
data_file 'WEAPON_METADATA_FILE' 'data/mypack_weaponarchetypes.meta'
data_file 'WEAPON_ANIMATIONS_FILE' 'data/mypack_weaponanimations.meta'
data_file 'WEAPONINFO_FILE' 'data/mypack_weapons.meta'
files { 'data/*.meta', 'html/img/items/*.webp' }
-- gunny.lua: runs when gunny is up, and again each time it starts
Gunny.register(function()
exports.whip_gunny:RegisterMagazine('mypack_mag_m4', {
weight = 0.2, label = 'M4 Magazine', prop = 'w_ar_mypack_m4_mag', caliber = '5.56x45', capacity = 30,
})
exports.whip_gunny:RegisterWeapon('mypack_m4', {
weight = 3.2, label = 'M4', description = 'A carbine.', hash = `WEAPON_MYPACK_M4`,
prop = 'w_ar_mypack_m4', caliber = '5.56x45', compatibleMags = { 'mypack_mag_m4' }, reloadTime = 2200,
attachments = { scope = { medium = `COMPONENT_MYPACK_M4_SCOPE` } },
})
end)- Print what each register returns while you build: a name already in use is refused, and an attachment kind no item in
items/attachments.luahas is reported. - When the pack stops, its items leave gunny; players keep them, and they work again when it's back.
- WHIP's weapon lists (the Director, the Object Viewer) show the guns once the pack calls
Whip.weaponsfrom@whip_core/lib/satellite.lua.
gunny's own fxmanifest.lua streams its add-on guns the same way and is a full example.
Debugging#
/w → Debug → Gunny Debug shows the drawn weapon's rounds as the game, gunny and the server count them, and the shot reports. /gunnydebugreset zeroes the counters; /gunnydebugserver says what's drawn. [GUNNY] WARNING lines always print: a failed write, or an undone change.
A gun that won't reload#
| The player sees | Why, and what to do |
|---|---|
| No magazines for this weapon on you | None of its compatibleMags is in their pockets |
| All your magazines for this weapon are empty | Load Bullets on one, with its caliber's rounds carried |
| No ammunition for this weapon on you | A gun with no magazine needs its caliber's loose rounds |
| No room for the old magazine | The old one had nowhere to go, not even the ground: the reload was undone |
| Nothing at all | R does nothing in a vehicle, with a melee weapon, with a screen or bar up, or while the gun is being put away |