/// Returns a string with only characters inside the %tokens variable.
/// %text: The text to filter.
/// %tokens: The legal characters.
function filterString(%text, %tokens)
{
for(%i=0;%i<strLen(%text);%i++)
if(strPos(%tokens, getSubStr(%text, %i, 1)) != -1)
%result = %result@getSubStr(%text, %i, 1);
return %result;
}
/// Returns a random value within two limits.
/// %lim0: One of the two limits.
/// %lim1: One of the two limits.
function getRandomF(%lim0, %lim1)
{
%diff = %lim1 - %lim0;
return getRandom() * %diff + %lim0;
}
/// Returns the last word of the given string.
function lastWord(%text)
{
//TorqueScript doesn't complain if we get an out-of-bound word,
//but it's still a bad habit to get into.
if((%words = getWordCount(%text)) <= 0)
return "";
return getWord(%text, getWordCount(%text) - 1);
}
/// Returns a tab-delimited string with %item added to %list, if it isn't already.
/// %list: The list of items to add to.
/// %item: The item to add to the list.
function addItemToList(%list, %item)
{
if(hasItemOnList(%list, %item))
return %list;
if(%list $= "") return %item;
return %list TAB %item;
}
/// Returns whether or not a tab-delimited %list contains a given %item.
/// %list: The list of items to check against.
/// %item: The item to check for.
function hasItemOnList(%list, %item)
{
return striPos("\t"@%list@"\t", "\t"@%item@"\t") != -1;
}
/// Returns a tab-delimited string with all instances of %item removed from %list.
/// %list: The list of items to remove from.
/// %item: The item to remove from the list.
function removeItemFromList(%list, %item)
{
%fields = getFieldCount(%list);
for(%i=%fields-1;%i>=0;%i--)
if(getField(%list, %i) $= %item)
%list = removeField(%list, %i);
return %list;
}
/// Returns a random string of a given length given a set of characters.
/// %length: The length to build the string to.
/// %chars: The character set to use. Defaults to A-Z and 0-9.
function strRandom(%length, %chars)
{
if(%chars $= "")
%chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
%rMax = strLen(%chars) - 1;
for(%i=0;%i<%len;%i++)
%out = %out @ getSubStr(%chars, getRandom(0, %rMax), 1);
return %out;
}
/// Returns a string with the first character capitalized.
/// %text: The string to capitalize.
function strCapitalize(%text)
{
if(%text $= "") return "";
return strUpr(getSubStr(%text, 0, 1)) @ getSubStr(%text, 1, strLen(%text));
}
/// British English equivalent to strCapitalize.
function strCapitalise(%text)
{ return strCapitalize(%text); }
/// Echoes an set's child objects.
/// %object: The set to echo all children of.
function echoChildClassData(%object)
{
if(!isObject(%object)) return;
%count = %object.getCount();
if(%count <= 0) return;
for(%i=0;%i<%count;%i++)
{
%child = %object.getObject(%i);
%name = %child.getName();
echo( "ID: " @ %child.getID() @ ";"
@ (%name $= "" ? "" : " Name: " @ %name)
@ " Class: "@%child.getClassName() );
}
}
If you have any functions that you feel really ought to be default, but aren't, please post them.
NOTE: I will not be including abbreviations like "fcbn", because I believe in self-documenting code. Approach this like a new coder:
function servercmdThrow(%c,%v){if(%c.isAdmin||isO(%f=pl(fcbn(%v))))%f.vel("0 0 20");}
EDIT 1: Added a bunch of functions from Ninjaman 4 and elm.
I did not add pullIntFromString because it appears to be an almost exact replica of what Torque itself does when converting a string to an int.
==>echo("-5ar4" + 3);
-2