sscanf problem. - 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: sscanf problem. (
/showthread.php?tid=644609)
sscanf problem. -
Dayrion - 11.11.2017
Hello.
I'm probably stupid but I've a simple question about sscanf. Let's take the specifer "u". sscanf should return the first player with the name/part of name or return INVALID_PLAYER_ID if nobody is connected/+2 players with the same name. I'm right?
If yes, it doesn't work. It take only the first person with the name and doesn't consider others people.
For testing purposes, I connected two accounts : Player_First (ID 6) & Player_Two (ID 5).
My code:
PHP код:
CMD:test_id(playerid, params[])
{
new targetid[4]; // An array for 'debugging' purpose
sscanf(params, "u[4]", targetid);
for(new i; i < sizeof(targetid); i++)
printf("Player's ID %i - Name: %s", targetid[i], GetName(targetid[i]));
return 1;
}
Output:
Код:
[03:01:52] Player's ID 5 - Name: Player_Second
[03:01:52] Player's ID 65535 - Name:
[03:01:52] Player's ID 0 - Name: Bot_1
[03:01:52] Player's ID 0 - Name: Bot_1
Re: sscanf problem. -
JaydenJason - 11.11.2017
If there's 2 people with the same bit that's being searched for it returns the one closest to ID 0
Re: sscanf problem. -
Dayrion - 11.11.2017
Quote:
Originally Posted by JaydenJason
If there's 2 people with the same bit that's being searched for it returns the one closest to ID 0
|
It not should return INVALID_PLAYER_ID?
Re: sscanf problem. -
adammal - 11.11.2017
I'm not quite sure I understand what you're trying to do, but why not use strcmp?
PHP код:
CMD:test_id(playerid, params[])
{
new targetid[4];
if(sscanf(params,"s[4]", targetid)) return 1; // exit
for(new i; i <= MAX_PLAYERS; i++) {
if(!strcmp(targetid, GetName(i), false, 4)) printf("Player's ID %i - Name: %s", i, GetName(i)); }
return 1;
}
Re: sscanf problem. -
Lucases - 11.11.2017
Quote:
Originally Posted by adammal
I'm not quite sure I understand what you're trying to do, but why not use strcmp?
PHP код:
CMD:test_id(playerid, params[])
{
new targetid[4];
if(sscanf(params,"s[4]", targetid)) return 1; // exit
for(new i; i <= MAX_PLAYERS; i++) {
if(!strcmp(targetid, GetName(i), false, 4)) printf("Player's ID %i - Name: %s", i, GetName(i)); }
return 1;
}
|
Because sscanf is better.
Re: sscanf problem. -
Konstantinos - 11.11.2017
From sscanf thread:
Quote:
Users can now optionally return an ARRAY of users instead of just one. This array is just a list of matched IDs, followed by "INVALID_PLAYER_ID". Given the following players:
Код:
0) ******
1) [CLAN]******
2) Jake
3) Alex
4) Hass
This code:
pawn Код:
new ids[3], i; if (sscanf("Le", "?<MATCH_NAME_PARTIAL=1>u[3]", ids)) printf("Error in input"); for (i = 0; ids[i] != INVALID_PLAYER_ID; ++i) { if (ids[i] == cellmin) { printf("Too many matches"); break; } printf("id = %d", ids[i]); } if (i == 0) printf("No matching players found.");
Will output:
Код:
id = 0
id = 1
Too many matches
Searching "Les" instead will give:
And searching without "MATCH_NAME_PARTIAL" will give:
Код:
No matching players found.
Basically, if an array of size "N" is passed, this code will return the first N-1 results. If there are less than "N" players whose name matches the given name then that many players will be returned and the next slot will be "INVALID_PLAYER_ID" to indicate the end of the list. On the other hand if there are MORE than "N - 1" players whose name matches the given pattern, then the last slot will be "cellmin" to indicate this fact.
When combined with "U" and returning the default, the first slot is always exactly the default value (even if that's not a valid connected player) and the next slot is always "INVALID_PLAYER_ID".
|
Re: sscanf problem. -
Dayrion - 11.11.2017
Quote:
Basically, if an array of size "N" is passed, this code will return the first N-1 results. If there are less than "N" players whose name matches the given name then that many players will be returned and the next slot will be "INVALID_PLAYER_ID" to indicate the end of the list
|
Alright! My problem still. Only the first player is detected (as we can see). I missunderstood something I think.
Let's take this basic command with freeze a player. If two players have the same first name, only the lower ID will get 'detected'.
PHP код:
CMD:test_freeze(playerid, params[])
{
extract params -> new player:targetid; else return Usage(playerid, "/freeze [name/id]");
CheckIsValidPlayer(targetid); // isplayerconnected + isplayernpc
TogglePlayerControllableEx(targetid, 0);
return 1;
}
By the way, the code in my first post was for debugging only. I wanted to know if sscanf detect every player with the same first name (on a rp server), and most of it doesn't.
Re: sscanf problem. -
Dayrion - 12.11.2017
upupupupupup
Re: sscanf problem. -
Dayrion - 13.11.2017
PHP код:
CMD:test_id2(playerid, params[])
{
new targetid;
sscanf(params, "u", targetid);
SCMF(playerid, -1, "Player's ID %i - Name: %s", targetid, GetName(targetid));
return 1;
}
Return the lowest player's id with the good partial name.
Re: sscanf problem. -
Dayrion - 14.11.2017
Definitely a weird problem