Skip to content

"u" specifier doesn't works #11

@Dayrion

Description

@Dayrion

Heey. I'm occuring a bug since a long time ago.
Copied/Pasted from a thread I wrote: http://forum.sa-mp.com/showthread.php?p=3953725#post3953725

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.

Y_Less asked a minimal repro but yes, nothing else.

Apologize, that's my first "minimal repro"..
With the minimal repro, the output is not exactly the same thing.

#include <a_samp>
#include <sscanf2>

public OnGameModeInit()
{
	ConnectNPC("Jina_Sawyers", "onfoot_test");
	ConnectNPC("Jina_Crawford", "onfoot_test");
	ConnectNPC("Jina_Scott", "onfoot_test");
	ConnectNPC("Bot_Tox", "onfoot_test");
	ConnectNPC("Bot_Tax", "onfoot_test");
	ConnectNPC("Bot_Tix", "onfoot_test");
	return 1;
}

public OnGameModeExit()
{
	return 1;
}

main() {}

public OnPlayerSpawn(playerid)
{
	if(playerid != 5) // maxnpc 6 in server.cfg
		return 1;

	new targetid_array[6],
		player_name[MAX_PLAYER_NAME], 
		targetid_value;

	print("Testing with : \"Jina_\"");
	sscanf("Jina_", "u[4]", targetid_array);
	for(new i; i < sizeof(targetid_array); i++) 
	{
		GetPlayerName(targetid_array[i], player_name, sizeof(player_name));
		printf("[ARRAY] Player's ID %i - Name: %s", targetid_array[i], player_name);
	}

	sscanf("Jina_", "u", targetid_value);
	GetPlayerName(targetid_value, player_name, sizeof(player_name));
	printf("[SINGLE VALUE] Player's ID %i - Name: %s", targetid_value, player_name);

	print("Testing with : \"Bot_\"");
	sscanf("Bot_", "u[4]", targetid_array);
	for(new i; i < sizeof(targetid_array); i++) 
	{
		GetPlayerName(targetid_array[i], player_name, sizeof(player_name));
		printf("[ARRAY] Player's ID %i - Name: %s", targetid_array[i], player_name);
	}

	sscanf("Bot_", "u", targetid_value);
	GetPlayerName(targetid_value, player_name, sizeof(player_name));
	printf("[SINGLE VALUE] Player's ID %i - Name: %s", targetid_value, player_name);
	return 1;
}

Output:

[CODE]Testing with : "Jina_"
[ARRAY] Player's ID 0 - Name: Jina_Sawyers
[ARRAY] Player's ID 1 - Name: Jina_Crawford
[ARRAY] Player's ID 2 - Name: Jina_Scott
[ARRAY] Player's ID 3 - Name: Bot_Tox
[ARRAY] Player's ID 4 - Name: Bot_Tix
[ARRAY] Player's ID 5 - Name: Bot_Tax
[ARRAY] Player's ID 65535 - Name: Bot_Tax
[SINGLE VALUE] Player's ID 0 - Name: Jina_Sawyers
Testing with : "Bot_"
[ARRAY] Player's ID 3 - Name: Bot_Tox
[ARRAY] Player's ID 65535 - Name: Bot_Tox
[ARRAY] Player's ID 2 - Name: Jina_Scott
[ARRAY] Player's ID 3 - Name: Bot_Tox
[ARRAY] Player's ID 4 - Name: Bot_Tix
[ARRAY] Player's ID 5 - Name: Bot_Tax
[ARRAY] Player's ID 65535 - Name: Bot_Tax
[SINGLE VALUE] Player's ID 3 - Name: Bot_Tox

I temporarily "solved" the problem by using kustom specifier.

ReturnUser(text[], playerid = INVALID_PLAYER_ID)
{ 
        new pos = 0;
        while (text[pos] < 0x21) // Strip out leading spaces
        {
                if (text[pos] == 0) return INVALID_PLAYER_ID; // No passed text
                pos++;
        }
        new userid = INVALID_PLAYER_ID;
        if (IsNumeric(text[pos])) // Check whole passed string
        {
            // If they have a numeric name you have a problem (although names are checked on id failure)
            userid = strval(text[pos]);
            if (userid >=0 && userid < MAX_PLAYERS)
            {
                if(!IsPlayerConnected(userid))  userid = INVALID_PLAYER_ID;
                else return userid; // A player was found
            }
        }

        new len = strlen(text[pos]);
        new count = 0;
        new name[MAX_PLAYER_NAME];
        foreach(new i : Player)
        {
                if (IsPlayerConnected(i))
                {
                        GetPlayerName(i, name, sizeof (name));
                        if (strcmp(name, text[pos], true, len) == 0) // Check segment of name
                        {
                                if (len == strlen(name)) // Exact match
                                {
                                        return i; // Return the exact player on an exact match
                                        // Otherwise if there are two players:
                                        // Me and MeYou any time you entered Me it would find both
                                        // And never be able to return just Me's id
                                }
                                else // Partial match
                                {
                                        count++;
                                        userid = i;
                                }
                        }
                }
        }
        if (count != 1)
        {
                if (playerid != INVALID_PLAYER_ID)
                {
                        if (count)
                        {
                                SendClientMessage(playerid, 0xFF0000AA, "Multiple users found, please narrow earch");
                        }
                        else
                        {
                                SendClientMessage(playerid, 0xFF0000AA, "No matching user found");
                        }
                }
                userid = INVALID_PLAYER_ID;
        }
        return userid; // INVALID_USER_ID for bad return
}

SSCANF:fix_u(player_name[MAX_PLAYER_NAME])
    return ReturnUser(player_name);

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions