Searching for Partial String
#1

Hello, I have recently started programming in pawn, and am trying to create a /su command that will search an array of strings (charges), and find the closest match if any. For this, I have used the ReturnUser function from the GF script (though modified), and can't figure out where I'm going wrong.

Here's a little test array I used for testing if this can work or not.
Код:
new crimeArray[600]={"Reckless Driving", "Vehicular Homicide", "vehicular manslaughter", "vehicular assault",
					 "hit and run", "water eluding", "air eluding"};
Here's the stock code where I think my problem lies. Keep in mind that this was used to search for partial names before in the GF script, so I haven't removed those comments:
Код:
stock ReturnSu(text[], playerid)
{
    new pos = 0;
    while (text[pos] < 0x21) // Strip out leading spaces
   	{
        if (text[pos] == 0) return 0; // No passed text
        pos++;
   	}
    
    // They entered [part of] a name or the id search failed (check names just incase)
    new len = strlen(text[pos]);
    new count = 0;
    new crimeID;
    new crime[MAX_PLAYER_NAME];
    for (new i = 0; i < sizeof(crimeArray); i++)
    {
     	if (strcmp(crimeArray[i], text[pos], true, len) == 0) // Check segment of name
      	{
       		if (len == strlen(text)) // Exact match
         	{
          		return i; // Return the exact player on an exact match
          	}
         	else // Partial match
          	{
           		count++;
             	crimeID = i;
           	}
       	}
    }
    if (count != 1)
    {
    	if (count)
     	{
      		SendClientMessage(playerid, 0xFF0000AA, "Multiple charges found, please narrow search.");
        }
        else
        {
        	SendClientMessage(playerid, 0xFF0000AA, "No matching charge found.");
        }
    }
    return crimeID; // INVALID_PLAYER_ID for bad return
}
And here is where I'm using that stock code in my actual program:
Код:
COMMAND:su(playerid, params[])
{
	new crimSearch, crimeID;
	if(!sscanf(params, "s[64]", crimSearch))
	{
	    crimeID = ReturnSu(crimSearch);
	    SCM(playerid, RED, crimeArray[crimeID]);
	}
	else SCM(playerid, YELLOW, "Usage: /su (player id/part of name)");
}
I am getting the argument mismatch error on "crimeID = ReturnSu(crimSearch);" As far as I can tell, I am passing it a string, which it should accept as an argument.

I use the zcmd format for commands, but from what I understand, the "s" in the sscanf line searches for the first string in the command, and assigns it to crimSearch. If you need any more information, feel free to ask.
Reply
#2

You're using "stock ReturnSu(text[], playerid)" and only using "ReturnSu(crimSearch);" I'm fairly sure it should be ReturnSu(crimSearch, playerid);

Edit: Also, if you're using /su (playerid), you're actually setting it to the player that executes that command!
Reply
#3

I have changed the code to be "crimeID = ReturnSu(crimSearch, playerid);" and it is still giving me the mismatch error. I'll be quite honest though, I don't understand exactly how this code works, or why it would be setting it to the player that executes the command.

From what I understand, I thought the player id who called the command in "COMMANDu(playerid, params[])" was set to "playerid". I just need to pass the id of he who called the command to the script, so that the script can send the messages that are part of the stock code itself.
Reply
#4

I've messaged you regarding this. I hope we can get it fixed!
Reply
#5

Could someone possibly explain why "text[]" has the brackets after it? Does this make the text an array? Like I said, I took this function from the GF script but don't fully understand it yet.
Reply
#6

Quote:
Originally Posted by Coolflip
Посмотреть сообщение
Could someone possibly explain why "text[]" has the brackets after it? Does this make the text an array? Like I said, I took this function from the GF script but don't fully understand it yet.
Yes, it makes it an array with the size depending on the parameter (which in this case the first parameter).
Reply
#7

In that case, exactly what am I supposed to pass to the function? The script before just passed it a getPlayerName return, so I figured it was a simple text comparison function.
Reply
#8

Change:

pawn Код:
COMMAND:su(playerid, params[])
{
    new crimSearch, crimeID;
    if(!sscanf(params, "s[64]", crimSearch))
    {
        crimeID = ReturnSu(crimSearch);
        SCM(playerid, RED, crimeArray[crimeID]);
    }
    else SCM(playerid, YELLOW, "Usage: /su (player id/part of name)");
}
to this:

pawn Код:
COMMAND:su(playerid, params[])
{
    new crimSearch[64], crimeID, idx;
    if(!sscanf(params, "is[64]", idx, crimSearch))
    {
        crimeID = ReturnSu(idx, crimSearch);
        SCM(playerid, RED, crimeArray[crimeID]);
    }
    else SCM(playerid, YELLOW, "Usage: /su (player id/part of name) (part of crime name)");
}
The thing is, by looking at that function, you must type part of the crime name. The usage example is:

Код:
/su 5 Homicide
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)