PDA

View Full Version : sscanf - optional number?


Jack_Leslie
07/10/2011, 07:25 AM
How do you have an optional number with sscanf?

I have,

new action[128], item[128], amount;

if(sscanf(params, "s[128]z[128]d", action, item, amount))
{
SendClientMessage(playerid, COLOR_GREY, "* Usuage: /car [syntax]");
SendClientMessage(playerid, COLOR_GRAD2, "* Syntax: Check");
SendClientMessage(playerid, COLOR_GRAD3, "* Syntax: Put - pot crack mats weapon");
SendClientMessage(playerid, COLOR_GRAD4, "* Syntax: Get - pot crack mats weapon");
return 1;
}


And then,

if(strcmp(action, "check") == 0)
{
if(!amount)
{
new pot, crack, mats, gun1, gun2, gun3;
pot = CarInfo[car][vPot], crack = CarInfo[car][vCrack], mats = CarInfo[car][vMats];
SendClientMessage(playerid, COLOR_GREY, "__________ Car Storage __________");
format(string, sizeof(string), "Crack: %d - Pot: %d - Materials: %d", crack, pot, mats);
SendClientMessage(playerid, COLOR_GRAD2, string);
}
return 1;
}


But if I do /car check then it doesn't work..

[MWR]Blood
07/10/2011, 07:28 AM
if(sscanf(params, "S[128]Z[128]D", action, item, amount))

Just put caps on the param(s) that you want to be optional.

Jack_Leslie
07/10/2011, 07:36 AM
Cheers mate, that worked. How do I check if nothing is entered in the option string if I want something to be entered?

I tried:
if(!strlen(item)) return SendClientMessage(playerid, COLOR_GREY, "* Usuage: /car put [item] [amount]");

But it didn't work, I also tried just if(!item) but that didn't work neither.

iggy1
07/10/2011, 07:39 AM
You mean you want default values on the params? or just check if params is null?

[MWR]Blood
07/10/2011, 07:39 AM
Use

if(isnull(params))

iggy1
07/10/2011, 07:42 AM
Default values are enclosed in parentheses.
if(sscanf(params, "S[128](default string)S[128](another string)D(500)", action, item, amount))

Result is; action = "default string", item = "another string" and amount = 500. If no params were entered. Also assuming your using the plugin.

Blood;1446396']Use

if(isnull(params))


He is already using sscanfs inbuilt null check in his code...

Jack_Leslie
07/10/2011, 07:46 AM
Default values are enclosed in parentheses.
if(sscanf(params, "S[128](default string)S[128](another string)D(500)", action, item, amount))

Result is; action = "default string", item = "another string" and amount = 500. If no params were entered. Also assuming your using the plugin.

action = needed string
item = optional string
amount = needed value if item is entered

I get undefined symbol on isnull, why? :S

iggy1
07/10/2011, 07:48 AM
action = needed string
item = optional string
amount = needed value if item is entered

I get undefined symbol on isnull, why? :S

DW your sscanf line IS checking if params is null. You dont need to use "isnull" in this case. BTW which command processor are you using?

Jack_Leslie
07/10/2011, 07:58 AM
DW your sscanf line IS checking if params is null. You dont need to use "isnull" in this case.

Oh right okay so I'm not using isnull, however, if I put a action item and no amount, the default amount is 114, but it should send the message syntax: /car put or whatever I want to send?