Author Topic: Help with findClientByName function  (Read 1615 times)

no see here's what you do
1: when you type in "an" it'll match anoob and then return anoob's client object
this is wrong
what it should do is this
loop through all people - if there is only one match, return the match - if there are more than one matches, return false or "" or something
it either returns a positive match or no match at all - you don't want to to (seemingly randomly) pick one person over the other

now how might you go about doing that - looping and making sure you don't have more than one result

Code: [Select]
function findClientByStartOfName(%name)
{
%matchCount = 0;
%numOfClients = clientGroup.getCount();

for(%count = 0; %count < %numOfClients; %count++)
{
%target = clientGroup.getObject(%count);

if(getSubStr(%target.name, 0, strLen(%name)) $= %name)
{
%matchedClient = %target;
%matchedCount++;
}
}

if(!(%matchedClient $= ""))
{
if(%matchedCount == 1)
{
return(%matchedClient);
}
}

return false;
}

Okay, so I think this does exactly what I want it to. This actually turned out to be a lot simpler than I first thought it would be, thanks for giving me direction on what to do.

But now for letting the player no what went wrong. Would it be better to have the function use %client as a parameter, so that way it can just message the player itself telling them what is wrong. Or would it be better to return numbers, so like 2 for multiple matches found and 3 for no match found. I think that would be harder because then I have to deal with it each time I call it but I don't really know.

if you were making a programming library you would have the multiple outputs as well as fantastic documentation

since you aren't doing that i would just drop in a "error client" argument like you were saying and manually handle the crap there
lousy coding practice but there isn't anything wrong with it technically

I just thought of another problem with the new findClientByStartOfName function, what if someone's name is "A" and someone is named "aNoob". It will never return the client for the player "A" will it? Would adding something in there so that if a search phrase matches a client name to return that client make sense?

I just thought of another problem with the new findClientByStartOfName function, what if someone's name is "A" and someone is named "aNoob". It will never return the client for the player "A" will it? Would adding something in there so that if a search phrase matches a client name to return that client make sense?
beautiful idea - i hadn't even thought of that exception

you've become sentient and graduated out of lugnuts school of programming congrats

Code: [Select]
function advancedFindClientByName(%name)
{
if(%name $= "")
{
return false;
}

%matchCount = 0;
%numOfClients = clientGroup.getCount();

for(%count = 0; %count < %numOfClients; %count++)
{
%target = clientGroup.getObject(%count);

if(getSubStr(%target.name, 0, strLen(%name)) $= %name)
{
%matchedClient = %target;
%matchedCount++;
}

if(%name $= %matchedClient.name)
{
return(%matchedClient);
}
}

if(!(%matchedClient.name $= ""))
{
if(%matchedCount == 1)
{
return(%matchedClient);
}
}

return false;
}

I realized on the last one I had my loops screwed up and it would return at the wrong time. So I think I fixed that, now I added that it will return the client if the search phrase completely matches the client name now. I think think this will work perfect now, I don't have anyone on a server to test this though.
« Last Edit: March 01, 2014, 02:00:53 PM by devildogelite »

What if the client whose name entirely matches comes after one whose name starts with a match?

What if the client whose name entirely matches comes after one whose name starts with a match?

Wouldn't that be handled since the if statement checking to see if it's a complete match is in the for loop?

Err.. oh wait, only really looked over the body of the for loop.