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.
Syntax
Section titled “Syntax”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 |
Returns
Section titled “Returns”any ... - the values in order, or false, reason
Example
Section titled “Example”-- /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 defaultlocal target, amount = sscanf(args, "u?d?")if target == false then return SendClientMessage(pid, COLOUR_RED, amount) end -- the reasontarget, amount = target or pid, amount or 30
-- /say <text>: a word, then the rest of the line wholelocal channel, text = sscanf(args, "sz")
-- /arena <x> <y> <z> [yaw]local x, y, z, yaw = sscanf(args, "ffff?")See also
Section titled “See also”OnPlayerCommandText · GetPlayerId · SendClientMessage · the Chat and commands group of the index
