Esc to clear, Enter to open the best match, click an export to open it in its page
whip_gunny

Learning from gunny

whip_gunny is the worked example of a resource built on WHIP: each section is a pattern WHIP expects, with gunny's code for it. New to WHIP? Start with Your First Resource.

Every client message is declared

Everything a game may send gunny's server is a SecureEvent or SecureCallback, declared inside Whip.register: whip_core checks the rate limit and each parameter before the handler runs.

exports.whip_core:SecureEvent('myguns:server:shotFired', {
    whileDead = true,                   -- a round fired as the player dies is still spent
    rateLimit = 15, burst = 30,
    params = { {'shotsFired', 'int', 1, 10, 1} },
}, function(source, player, shotsFired) --[[ ... ]] end)

'int' holds a value in range (9999 counts as 10) instead of refusing it: the rounds were fired, so the count mustn't be refused. Use 'amount' for money or items, which refuses, and 'id' for an exact id. gunny's RegisterGunnyEvent and RegisterGunnyCallback wrap the same call only because a weapon pack changes what its 'slot' params accept.

Declare inside Whip.register

whip_core keeps items, callbacks, panels and hooks in memory. Declare them inside Whip.register (from @whip_core/lib/satellite.lua) and they're declared once whip_core is ready; with dependency 'whip_core', a whip_core restart restarts you too, so there's nothing to clean up (the satellite helper).

Name the item the player clicked with a 'slot' param

The client sends the slot; a 'slot' param checks the pocket is the player's and holds the right kind of item, and hands over { addr, item, amount, metadata }. An item that moved meanwhile is refused with a readable reason.

-- server/magazines.lua
RegisterGunnyCallback('whip_gunny:unloadBulletsFromMag', { params = {
    { 'mag',   'slot', { item = GunnyMagazineItems } },   -- one of the player's own magazines
    { 'count', 'int',  1, 9999, 9999 },
} }, function(source, player, mag, count)
    local ok, left, unloaded = GunnyUnloadMagazine(source, mag.addr, count)
    if not ok then return false, left end
    return true, { left = left, unloaded = unloaded }
end)

The game learns the slot from a right-click action (RegisterContextAction), which gets addr and tells the game addr.slot and addr.sub. Prefix every action, event and callback with your resource's name: names are server-wide, and an action's is at most 32 characters.

Declare a panel, don't handle one

The magazine slot (server/magpanel.lua) declares how it reads and writes the gun's metadata and what may go in (onValidate); whip_core performs the move and calls onChange. A panel that only holds items needs only allowedItems. Either way the item must not stack and needs an action that opens it (the worked example).

Take before you give, and check every result

A reload takes the new magazine before giving the old one back; unloading lowers the count before giving the rounds. A failed step gives back what was taken (GunnyGiveBack), and GunnyStillThere reads the slot again after any wait so the undo lands on the same item.

-- server/magazines.lua: unloading
meta.ammo = currentAmmo - toUnload                            -- take: the magazine lowered first
if not exports.whip_core:SetItemMetadata(magAddr, meta) then
    return false, 'Could not unload: the magazine was not changed'
end
local added = Whip.core:AddItem(source, caliber.ammoItem, toUnload,   -- then give
    nil, { reason = 'unloaded from a magazine', noDrop = true })
if not added then
    -- no room: the rounds go back into the magazine, if it's still the one lowered
end

A bar the server times

Loading a magazine runs the server Progress through Whip.core and loads only when it returns true; a finish sooner than the bar could fill is refused ('too fast'). It waits, so run it from a SecureEvent or right-click action, never a SecureCallback (the game gives up after 10 seconds), and read the item again after. To use an item at the end of a bar, give RegisterUseHandler a table instead (a use with a progress bar).

Go by the server's own record

Shots, throws and holstering go by where the server saw the gun drawn and its serial (GunnyDrawnWeapon), never a slot the client names. Every gun and magazine gets a serial as it's made (RegisterItemDefaultMetadata). Give your own findable items an id the same way: WHIP's uid only comes the first time an item is logged.

Calling whip_core

Call an export straight when it returns at once, inside a handler whip_core runs (GetItemAt, SetItemMetadata). Use Whip.core for a call that can wait (AddItem, Progress), for everything after such a wait, and outside whip_core's handlers; it returns false, why instead of throwing. The full rule is in Calling whip_core, and at the top of gunny's server/main.lua and client/main.lua.

Where to look#

Read server/main.lua, server/items.lua, server/weapons.lua, server/magpanel.lua, then client/main.lua.

To see how toRead
Register items, right-click options and panelsserver/items.lua: registerWithWhip
Declare what the game may askserver/main.lua: RegisterGunnyCallback, RegisterGunnyEvent; server/shooting.lua
Act on the item the player clickedserver/magazines.lua: unloadBulletsFromMag; client/main.lua: GunnySlot
Run a bar the server timesserver/magazines.lua: runLoad
Take before you give, and undo on a failureserver/reload.lua: performReload; server/main.lua: GunnyGiveBack, GunnyStillThere
Keep a record the server trustsserver/weapons.lua: equipWeapon, GunnyDrawnWeapon
Ask the server and wait for the resultclient/main.lua: GunnyCallback; client/weapons.lua: DrawWeapon
Run a per-frame loop only while neededclient/main.lua: GunnyStartTick, RegisterGunnyTick
Hold controls off for a momentclient/reload.lua: holdFire (BlockControls)
Every file
FileWhat's in it
config.lua, items/*.luaThe weapon bench; every caliber, weapon, magazine and attachment
shared/catalog.lua, shared/whip_weapons.luaItem lookups and rules; gunny's guns in whip_core's weapon lists
shared/packs.lua, lib/pack.luaWeapon packs
server/main.lua, client/main.luaDeclarations and asking the server, the drawn weapon, the per-frame tick, the calling rule
server/items.luaItem registration; GiveWeapon, GiveMagazine
server/weapons.lua, client/weapons.luaDrawing and putting away
server/magpanel.lua, */magazines.lua, */reload.luaThe magazine slot; loading and unloading; reloading
*/shooting.lua, */throwables.lua, client/ammo.lua, client/vehicle.luaShots and throws, the jerry can, sticky bombs; the one place ped ammo is written; shooting from a seat
*/attachments.lua, html/, */debug.luaThe weapon bench; Gunny Debug
data/, stream/, stream_enhanced/, audiodirectory/GTA weapon data, models and sounds (stream_enhanced/ for GTA V Enhanced); sources in anim_src/, audio_src/, props_src/