You'll find sscanf being usefull in quite alot more situations. For starters, in commands.
Assume you've the command "/warn". You'll want to specify a playerid (or username) and a reason:
Command /kick, with a kick reason being optional:
Or fetching someone's geolocation using ip-api (csv): (eg. ip-api.com/csv/IP_HERE?fields=country,countryCode)
Or if you want to make a database migration system (run migrations when booting your mode to ensure database is up-to-date, without having to manually do that).
Or maybe some motd data was weirdy saved in an ini file (eg. "motd=Msg1:Hello world!;Msg2:This is a message in the motd!;Msg3:Check our website!")
And I could go on for a while with crazier examples... but that's a bit too much (in fact, this entire post is because your answer has already been answered. Going waay off topic here).
Have a nice day :-)
To be more on topic, by the way:
1- "1 <= ip_part4 <= 255" will be true when ip_part4 is ATLEAST 1 and NO MORE THAN 255
2- "ip_part4 == 1" will be true when ip_part4 is EXACTLY 1.
Statement is double (and thus quite unnecessary) since both checks will return true for "1". Small thing, but wanted to point it out anyway.
Assume you've the command "/warn". You'll want to specify a playerid (or username) and a reason:
Code:
CMD:warn(playerid, params[])
{
? ? new warned_id, reason[75];
? ? if (sscanf(params, "rs[75]", warned_id, reason))
? ? {
? ? ? ? return SendClientMessage(playerid, -1, "USAGE: /warn [playerid/name] [reason]");
? ? }
? ? //Rest of code (warn the user, etc)
Command /kick, with a kick reason being optional:
Code:
CMD:kick(playerid, params[])
{
? ? new kicked_id, reason[75];
? ? if (sscanf(params, "rS(No reason)[75]", kicked_id, reason))
? ? {
? ? ? return SendClientMessage(playerid, -1, "USAGE: /kick [playerid/name] (reason. Default is 'No reason')");
? ? }
? ? //Rest of code...
Or fetching someone's geolocation using ip-api (csv): (eg. ip-api.com/csv/IP_HERE?fields=country,countryCode)
Code:
public on_fetch_geodata(playerid, response_code, data[])
{
? ? new
? ? ? ? country[64],
? ? ? ? countryCode[3]
? ? ;
? ? sscanf(data, "p<,>s[64]s[3]", country, countryCode);
? ? printf("Player %s(%i) is from %s(%s)", PlayerName(playerid), playerid, country, countryCode);
}
Or if you want to make a database migration system (run migrations when booting your mode to ensure database is up-to-date, without having to manually do that).
Code:
hook OnGameModeInit() //An OnGameModeInit() (or even OnScriptInit()) before any functions are loaded (but MySQL initialised)
{
? ? //Running functions to scan all files in "scriptfiles/db-migrations" (possible, using JaTochNietDan's FileManager. Code not here because purpose is sscanf example
? ? new
? ? ? ? migr_name[25], //Migration's name
? ? ? ? migr_v_maj, //Version (major)
? ? ? ? migr_v_min, //Version (minor)
? ? ? ? migr_v_bld //Version (build)
? ? ;
? ? //Let's assume a migration's name can be "NAME_vMaj-vMin-vBld" OR "NAME-vMaj" OR "NAME"
? ? if (!sscanf(file_name, "P<_-->s[25]ddd", migr_name, migr_v_maj, migr_v_min, migr_v_bld))
? ? {
? ? ? ? printf("Running migration '%s' from version %i.%i.%i", migr_name, migr_v_maj, migr_v_min, migr_v_bld);
? ? ? ? //mysql_query_file(___)
? ? }
? ? else if (!sscanf(file_name, "p<->s[25]i", migr_name, migr_v_maj))
? ? {
? ? ? ? printf("Running migration '%s' from version %i", migr_name, migr_v_maj);
? ? ? ? //etc...
? ? }
? ? else if (!sscanf(file_name, "p<->s[25]", migr_name))
? ? {
? ? ? ? printf("Running migration '%s, no version specified'", migr_name);
? ? ? ? //etc
? ? }
? ? else
? ? {
? ? ? ? printf("<!> Not running migration '%s', invalid file name", file_name);
? ? }
? ? return 1;
}
Or maybe some motd data was weirdy saved in an ini file (eg. "motd=Msg1:Hello world!;Msg2:This is a message in the motd!;Msg3:Check our website!")
Code:
new LineFromIniFile[] = "motd=Msg1:Hello world!;Msg2:This is a message in the motd!;Msg3:Check our website!"; //Represent that line I mentioned above
new Msg1[35], Msg2[35], Msg3[35];
sscanf(LineFromIniFile, "P<=:;:;:>{s[5]s[5]}s[35]{s[5]}s[35]{s[5]}s[35]", Msg1, Msg2, Msg3);
printf("%s----%s----%s", Msg1, Msg2, Msg3); //Output: Hello world!----This is a message in the motd!----Check our website!
And I could go on for a while with crazier examples... but that's a bit too much (in fact, this entire post is because your answer has already been answered. Going waay off topic here).
Have a nice day :-)
To be more on topic, by the way:
Quote:&& (1 <= ip_part4 <= 255 || ip_part4 == 1)If the IP is "127.0.0.1", ip_part4 will be "1".
1- "1 <= ip_part4 <= 255" will be true when ip_part4 is ATLEAST 1 and NO MORE THAN 255
2- "ip_part4 == 1" will be true when ip_part4 is EXACTLY 1.
Statement is double (and thus quite unnecessary) since both checks will return true for "1". Small thing, but wanted to point it out anyway.