Expanding consoleFunctions - isWordInSet?
by Fyodor -bank- Osokin · 08/17/2006 (12:04 pm) · 1 comments
In my current project I need a lot of checking if a special word exists in some string (words set).
e.g. - I have a string with values : "one two seven ten"
I need to check if word "four" is in.
How do I do it?
Yes, I can make a for() loop and use getWord() and do comparision. But when server doing it a lot, it takes some processor time parsing a script, so here is a C++ code of this function. This can be as example of HOWTO expanding Torque.
Open engine/console/consoleFunctions.cc and in a group of ConsoleFunctionGroupBegin( FieldManipulators... add the following:
Will you find this useful or not - it's up to you.
e.g. - I have a string with values : "one two seven ten"
I need to check if word "four" is in.
How do I do it?
Yes, I can make a for() loop and use getWord() and do comparision. But when server doing it a lot, it takes some processor time parsing a script, so here is a C++ code of this function. This can be as example of HOWTO expanding Torque.
Open engine/console/consoleFunctions.cc and in a group of ConsoleFunctionGroupBegin( FieldManipulators... add the following:
ConsoleFunction(isWordInSet, bool, 3, 3, "(string text, string word)")
{
U32 units = getUnitCount(argv[1], " \t\n");
for (U32 inc=0;inc<units;inc++)
if (dStricmp(getUnit(argv[1], inc, " \t\n"),argv[2])==0)
return true;
return false;
}The use of it is simple:%text = "one two seven ten";
%word = "four";
if (isWordInSet(%text, %word)) {
warn(%word SPC "is NOT in set" SPC %text);
} else {
echo(%word SPC "is in" SPC %text);
}I've just moved the whole manipulation from script to c++ code for making engine to work faster. It's a moments of saved time, but in total you can win a lot.Will you find this useful or not - it's up to you.
About the author
Game developer.

Associate David Higgins
DPHCoders.com