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

I'm trying to make it so when I call a server command it can do two different things. Pretty much function overloading but I'm pretty sure torque doesn't allow for  function overloading.

Code: [Select]
function serverCmdTest(%client, %target)
{
%target = findClientByName(%target);
if(isObject(%target))
{
messageClient(%client,'',"It's a player!");
return;
}
messageClient(%client,'',"It's not a player!");
}

Now this will work and tell me if there's a player for pretty much all cases. My username is ProletariatPatryk and it finds a player when I do /test a. That's the only time it doesn't work. Is there some part of my username I'm missing that has "a" in it?

EDIT: I just figured out why it was happening, it just looks for any letter in the player's name. Is there a way to make it so it only uses parts of the player name starting from the beginning. So like p, pr, pro and so on like that?
« Last Edit: February 28, 2014, 01:28:51 PM by devildogelite »

Prolet a ri a tP a tryk
those parts

make a manual function that uses strStr on all clients names
or strPos
if the string position is greater than 0 the string isn't the first letters

seems like I'd be a better idea to use getSubStr

seems like I'd be a better idea to use getSubStr
still only tells you if it's in the string, not where in the string, right?

still only tells you if it's in the string, not where in the string, right?

getSubStr(%name, 0, strLen(%test)) $= %test

???

I just realized my problem is actually with findClientByName. So from what I'm getting here is that you can make sure the phrase you enter is actually part of the name, but how do you make sure findClientByName actually does it? Because from what I can see is findClientByName just looks for any similarity it can.

You make sure by making your own findClientByName function. You'll need to go through each client in the ClientGroup group and do your desired comparison with their name, returning the client if it matches.

You make sure by making your own findClientByName function. You'll need to go through each client in the ClientGroup group and do your desired comparison with their name, returning the client if it matches.

Does it use ClientGroup.getLength() to get the number of clients, then go through each one with ClientGroup.getObject(%i) and do comparisons of that and the string entered?


There's no need for function overloading in torque since all variables are passed as strings

Code: [Select]
function findClientByName(%name)
{
%numOfClients = clientGroup.getCount() - 1;

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

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

I'm pretty sure this works exactly like how I want, but I don't have anyone else to test it with. So any input would be appreciated.

There's no need for function overloading in torque since all variables are passed as strings

I didn't realize you could call functions without filling all parameters when I started doing this. Also no datatypes is super annoying

Change <= in the for statement to <
I'm also not convinced that getSubStr is the best way to go, the code right now means you have to type the name in exactly as it is, every single character. I would use strPos and check to make sure the length of the search string is longer than one letter

I didn't realize you could call functions without filling all parameters when I started doing this. Also no datatypes is super annoying
It simplifies everything, which is actually pretty useful for people learning to script. Function overloading for every data type can get pretty tedious, especially if you don't knowwhy you're doing it because you don't have access to the memory storage anyway

Change <= in the for statement to <

Wait wouldn't I want it to be <= since I set numOfClients equal to the number of clients - 1?

I'm also not convinced that getSubStr is the best way to go, the code right now means you have to type the name in exactly as it is, every single character. I would use strPos and check to make sure the length of the search string is longer than one letter

Hmmm, typing in p, pr, pro etc  worked for proletariatpatryk. I'm confused by what you mean for the rest of this though. Could you give me an example of what you mean.

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

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

if(getSubStr(%target.name, 0, strLen(%name)) $= %name)
{
return(%target);
}
}
}
fixed it so you aren't overwriting a default frigging function
also so you use %i instead of %count which is coding practice or some dumb thing - no actual issue there
also removed the -1 and the <= because why the hell would you even overcomplicate like that

ps: your code still isn't perfect - suppose you have "anoob" and "anakin skywalker" or something and you type in "an"
1: what happens?
2: is this desired behavior?
3: can you fix this if it isn't desired? (hint it's probably not)

fixed it so you aren't overwriting a default frigging function
Yeah for the function name I just threw that there because I couldn't think of another name. I probably would have forgotten to change it when I moved along though.

also so you use %i instead of %count which is coding practice or some dumb thing - no actual issue there
I know %i is the normal way to do it. I just don't like doing it for some reason. I mean I do it on assignments for class but when it's a personal project I like making it a bit more descriptive. Don't know why, normally I just use count if I'm trying to be more descriptive which really doesn't make it more descriptive.


also removed the -1 and the <= because why the hell would you even overcomplicate like that
I end up overcomplicating things like that all the time. I start trying to make it more elegant or something and either end up with a good method or some round-about way like that, that just adds in more ways for something to go wrong.

ps: your code still isn't perfect - suppose you have "anoob" and "anakin skywalker" or something and you type in "an"
1: what happens?
2: is this desired behavior?
3: can you fix this if it isn't desired? (hint it's probably not)
Okay now to the real problem. I don't know how to fix this, I'm really confused. I'm thinking the best way is making it so the name you search for has to be a certain length. But then if someone has a one character name it won't work. I really don't know how to fix this without requiring a longer name to be entered. The only other way I can think of is using ID instead. If you could point me in the direction of an answer I would greatly appreciate it.