Author Topic: Finding the closest player to your request (Function) (Client-Sided)  (Read 1011 times)

function FindClosetName(%cl)
{
   %names = 0;
   for(%a=0; %a<npl_list.rowcount(); %a++)
   {
      if(strPos(getfield(npl_list.getrowtext(%a), 1),%cl) > -1)
      {
         %name = getfield(npl_list.getrowtext(%a), 1);
         %names++;
      }
   }
   if(%names > 0)
      return %name;
   else
      return 0;
}


If it returns as 0, it means it couldn't find it. Otherwise, it returns the full name that was requested.

The thing I cannot fix, is that it can be case sensitive. I put in FindClosetName("vis"); and it puts 0, then I put in FindClosetName("Vis"); and it says Visolator. Not sure if it is me or just torque's fault.

function FindClosetName(%cl)
FindClosetName
Closet


Use striPos, thats case insensitive.

strpos(strUpr(%name),strUpr(%cl))
striPos isn't default iirc

int strPos(var stringhay, var stringneedle)
Used to locate the first occurrence of stringneedle in stringhay.

TorqueScript in general is not case sensitive, but strPos is one of those few rare exceptions.

I use this:
Code: [Select]
function newPlayerListGui::finishName(%this, %name)
{
%this.finishNameError = 0;

if(strReplace(%name, " ", "") $= "")
{
return "";
}

%count = NPL_List.rowCount();

if(!%count)
{
return "";
}

for(%i = 0; %i < %count; %i++)
{
%rowName = getField(NPL_List.getRowText(%i), 1);

if(getSubStr(%rowName, 0, strLen(%name)) $= %name)
{
%matchingCount++;
%match = %rowName;
}
}

if(%matchingCount > 1)
{
%this.finishNameError = 2;

return "";
}

if(%match $= "")
{
%this.finishNameError = 1;

return "";
}

return %match;
}
If it returns blank, an error occurred. If newPlayerListGui.finishNameEr ror is 1, no name was found starting with the input. If newPlayerListGui.finishNameEr ror is 2, multiple names were found that start with the input.
I use a variable in newPlayerListGui because of the very unlikely chance that the player's name is 1 or 2.

Example of it being used:
Code: [Select]
%oldName = %name;
%name = newPlayerListGui.finishName(%name);

if(%name $= "")
{
if(newPlayerListGui.finishNameError == 1)
{
newChatHUD_addLine("Error. No names found starting with" SPC %oldName @ ".");
}

else if(newPlayerListGui.finishNameError == 2)
{
newChatHUD_addLine("Error. Multiple names found starting with" SPC %oldName @ ".");
}

return;
}
« Last Edit: November 02, 2013, 09:40:09 AM by jes00 »

striPos isn't default iirc

Holy stuff, no it isn't.

How and why on earth do I use it all the time?

But yeah, the easiest solution is to strUpr the name and search text.

Holy stuff, no it isn't.

How and why on earth do I use it all the time?

But yeah, the easiest solution is to strUpr the name and search text.

It's part of libstr. http://forum.blockland.us/index.php?topic=177446.0