SA-MP Forums Archive
SSCANF2 question - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: SSCANF2 question (/showthread.php?tid=464959)



SSCANF2 question - newbie scripter - 20.09.2013

i just saw some scripting helps
and i found this
if(sscanf(params,"sz",msg)) IN
pawn Код:
new string[128],msg[128],pname[MAX_PLAYER_NAME];
        GetPlayerName(playerid,pname,sizeof(pname));
        if(sscanf(params,"sz",msg)) return SendClientMessage(playerid,COLOR_WHITE, "USAGE: /me <text>");
        format(string,sizeof(string),"* %s %s",pname,msg);
        SendClientMessageToAll(COLOR_LIGHTBLUE,string);
AND

if(sscanf(params, "s[30]", veh)) in
pawn Код:
CMD : spawncar(playerid, params[])
{
if(pInfo[playerid][pLevel] >= 2)
{
new veh[30],vehid;
if(sscanf(params, "s[30]", veh)) return SendClientMessage(playerid, red, "Usage: /spawncar <Model ID/Vehicle Name>");
if(IsNumeric(veh)) vehid = strval(veh);
else vehid = ReturnVehicleModelID(veh);
if(vehid < 400 || vehid > 611) return SendClientMessage(playerid, red, "Invalid vehicle model!");
SpawnVehicle(playerid,vehid);
CommandToAdmins(playerid,"spawncar");
return 1;
}
else return ShowMessage(playerid, red, 1);
In this What Does "sz" and "s[30]" mean??


Re: SSCANF2 question - Konstantinos - 20.09.2013

Quote:
Originally Posted by SA:MP Wiki
The "z" format parameter when used at the end of a string will create an optional string parameter. It behaves exactly the same as the "s" parameter, especially when used in parts of the format string other than the end, but if the text passed is not long enough the sscanf function still returns 0 for true
I'm not sure though if it's still used, you'll need to check sscanf's thread.

s specifier is for strings. It needs to be followed with its size like the second example you gave.


Re: SSCANF2 question - JimmyCh - 20.09.2013

the "s" is for string, you must put the size of it aswell inside sscanf.
For example, if you did new veh[30]; you must put s[30] inside the sscanf.

You can use sscanf for all kinds of commands, for example if you need to set your skin to something, you can make a /setskin like the following(Just giving an example, do not use this):
pawn Код:
CMD:setmyskin(playerid, params[])
{
new skin, string[128];
if(sscanf(params, "d", skin)) return SendClientMessage(playerid, -1, " /setmyskin [0-300]");
if(skin < 0 || skin > 300) return SendClientMessage(playerid, -1, "Invalid skin!");
SetPlayerSkin(playerid, skin);
format(string, sizeof(string), "You have changed your skin to %d", skin);
SendClientMessage(playerid, -1, string);
return 1;
}
Hope you understand how it's used.


Re: SSCANF2 question - Konstantinos - 20.09.2013

Quote:
Originally Posted by JimmyCh
Посмотреть сообщение
pawn Код:
new skin[16], string[128];
if(sscanf(params, "d", skin)) return SendClientMessage(playerid, -1, " /setmyskin [0-300]");
In that case, skin needs to be declared as an integer.

pawn Код:
new skin, string[128];
if(sscanf(params, "d", skin)) return SendClientMessage(playerid, -1, " /setmyskin [0-300]");



Re: SSCANF2 question - JimmyCh - 20.09.2013

My bad with the size, didn't notice I put it there, edited.

Thanks for pointing it out


Re: SSCANF2 question - newbie scripter - 20.09.2013

we can either use "d" or "i" ?? https://sampwiki.blast.hk/wiki/Fast_Commands#sscanf

i forgot to ask whats the Use Of Msg and Veh in if(sscanf...


Re: SSCANF2 question - JimmyCh - 20.09.2013

That's correct, Sscanf code on the wiki shows the following codes that can be used:
Код:
The format codes are:
 
	c - A character.
	d, i - An integer.
	h, x - A hex number (e.g. a colour).
	f - A float.
	s - A string.
	z - An optional string.
	pX - An additional delimiter where X is another character.
	'' - Encloses a litteral string to locate.
	u - User, takes a name, part of a name or an id and returns the id if they're connected.
More info on the Wiki >> sscanf.


Re: SSCANF2 question - Konstantinos - 20.09.2013

Quote:
Originally Posted by newbie scripter
Посмотреть сообщение
we can either use "d" or "i" ?? https://sampwiki.blast.hk/wiki/Fast_Commands#sscanf

i forgot to ask whats the Use Of Msg and Veh in if(sscanf...
Yes, you can.

Quote:
Originally Posted by SA:MP Wiki
Sscanf is string splitting routine
So when you do:
pawn Код:
// in /skin command
new skin;
if(sscanf(params, "d", skin)) // return an error message (syntax)
That means that when you do /skin command, it needs to have an integer as a parameter after space.

/skin 10 (valid)
/skin 10.0 (invalid, it will return the syntax)
/skin test (invalid, it will return the syntax).

It's pretty much the same for the strings (like the examples of msg and veh.

In a command, /v infernus (infernus is the string it expected, we can use up to 30 characters. If you exceed them, it will give a warning in the console).

The specifiers can be compined.

pawn Код:
// in a /test command
new
    integer_,
    Float: float_,
    string[ 64 ]
;
if( sscanf( params, "dfs[64]", integer_, float_, string_ ) ) // return syntax: /test <number> <float> <text>
/test 10 50.0 haha (valid)
/test this is a text 10 50.0 (invalid, it expects the parameters with order Integer, Float, String.

NOTE: It's recommended if you want to use more than 1 parameter to use string at the end as it breaks the text when it finds a space.


Re: SSCANF2 question - newbie scripter - 20.09.2013

Ty and recently something happened and i couldnt test my server or script (when i open samp-server in end i see Run time error: 19 "File OR function is not found". i worked before ( i tried grandlarc and it worked only my script failes)


Re: SSCANF2 question - Konstantinos - 20.09.2013

That means that you miss some plugin(s). Load nativechecker plugin and you'll see what functions are not registered (it will print them in the console/server log). Then you'll be able to know what plugin you need to load.