Skip to content

sscanf

The arguments of a command as typed values - a player, a number, a word, the rest of the line - in one call.

The name is SA-MP’s on purpose: the same job, one letter per argument. args is the string OnPlayerCommandText hands you; format is one letter per argument, in order:

letter the argument what you get
u a player: a pid or a name (or a fragment of one), as GetPlayerId reads it the pid
d a whole number (-3, 50) a number
f a number (0.5, 12) a number
s one word - or a "quoted string" taken whole a string
z the rest of the line, from here to the end a string

A ? after a letter makes that argument optional: nil when it is not there. Words are split on spaces; whatever follows the last letter is ignored. It returns the values in the format’s order. When a required argument is missing or does not read - a number that is not one, a player nobody is called - it returns false and the reason ("argument 2 (abc) is not a whole number", "no player called xyz"), ready for a usage line: check the first value against false, not against nil, when the first letter is optional.

sscanf(args, format)
Parameter Type
args string the command’s arguments as OnPlayerCommandText gives them
format string one letter per argument (u d f s z), a ? after a letter for an optional one

any ... - the values in order, or false, reason

-- /pay <player> <amount>
local target, amount = sscanf(args, "ud")
if target == false then return SendClientMessage(pid, COLOUR_RED, "usage: /pay <player> <amount>") end
-- /hurt [player] [amount]: both optional, the caller and 30 by default
local target, amount = sscanf(args, "u?d?")
if target == false then return SendClientMessage(pid, COLOUR_RED, amount) end -- the reason
target, amount = target or pid, amount or 30
-- /say <text>: a word, then the rest of the line whole
local channel, text = sscanf(args, "sz")
-- /arena <x> <y> <z> [yaw]
local x, y, z, yaw = sscanf(args, "ffff?")

OnPlayerCommandText · GetPlayerId · SendClientMessage · the Chat and commands group of the index