Author Topic: [RESOURCE] Functions That Should Be Default (but Aren't)  (Read 6252 times)

Code: (Extended Defaults.cs) [Select]
/// 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
« Last Edit: June 14, 2014, 04:03:44 AM by Xalos »

getRandom already accepts a min and max value.

getRandom( min, max )

getRandom already accepts a min and max value.

getRandom( min, max )
That returns integer values, not floats.

That returns integer values, not floats.
That's true. He never said it returns a float though, I guess I should have actually read the code.

the findClient function
port wrote it, I have no idea where it is though.

That's true. He never said it returns a float though, I guess I should have actually read the code.

getRandomF

"Returns a value (not integer) between X and Y"

Ok, so if I'm correct, a float is a number that has a decimal value, and an integer is a negative or positive whole number. So then why does getRandom(); (without arguments) return a float, while getRandom(%lim0, %lim1) returns an integer?

Ok, so if I'm correct, a float is a number that has a decimal value, and an integer is a negative or positive whole number.
More often than not, yes. But floats can also be pure positive or negative integers.

More often than not, yes. But floats can also be pure positive or negative integers.
A whole number float isn't really a thing in languages with weak typing like TS

A whole number float isn't really a thing in languages with weak typing like TS
%x = 5.0;
%x = 1.5;
%x -= 0.5;

More often than not, yes. But floats can also be pure positive or negative integers.
Thanks!

Anyways, back OT:
(from RTB)
Code: [Select]
function lastWord(%string)
{
   return getWord(%string,getWordCount(%string)-1);
}

//- addItemToList (adds an item to a space delimited list)
function addItemToList(%string,%item)
{
if(hasItemOnList(%string,%item))
return %string;

if(%string $= "")
return %item;
else
return %string SPC %item;
}

//- hasItemOnList (checks for an item in a list)
function hasItemOnList(%string,%item)
{
for(%i=0;%i<getWordCount(%string);%i++)
{
if(getWord(%string,%i) $= %item)
return 1;
}
return 0;
}

//- removeItemFromList (removes an item from a space-delimited list)
function removeItemFromList(%string,%item)
{
if(!hasItemOnList(%string,%item))
return %string;

for(%i=0;%i<getWordCount(%string);%i++)
{
if(getWord(%string,%i) $= %item)
{
if(%i $= 0)
return getWords(%string,1,getWordCount(%string));
else if(%i $= getWordCount(%string)-1)
return getWords(%string,0,%i-1);
else
return getWords(%string,0,%i-1) SPC getWords(%string,%i+1,getWordCount(%string));
}
}
}

function getRandomString(%length)
{
   %numeroalphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
   
   for(%i=0;%i<%length;%i++)
   {
      %string = %string@getSubStr(%numeroalphabet,getRandom(0,strlen(%numeroalphabet)-1),1);
   }
   return %string;
}
I use the list functions a lot.

%x = 5.0;
%x = 1.5;
%x -= 0.5;

By 'not a thing' I meant 'the differentiation between 5 and 5.0 doesn't really matter'

function capFirstLetter(%string)
{
   if(%string !$= "")
      return strUpr(getSubStr(%string,0,1)) @ getSubStr(%string,1,strLen(%string));
}   

function echoChildClassData(%object)
{
   if(isObject(%object))
   {
      %count = %object.getCount();
      
      if(%count > 0)
         for(%i=0;%i<%count;%i++)
         {
            %child = %object.getObject(%i);
            
            if(isObject(%child))
               if(%child.getClassName() !$= "")
                  echo("ID: " @ %child @ " Name: " @ strReplace(%child.getName(),"",0) @ " CN: " @ %child.getClassName());
            
         }
   }
}


Edit: This one below can most likely be optimized, but i'm too lazy to do it:


function pullIntFromString(%this)
{
   %len = strLen(%this);
   %build = "NULL";
   
   for(%i=0;%i<%len;%i++)
   {
      %letter = getSubStr(%this,%i,1);
      
      if(%letter !$= "-" && (%letter * 1 == 0 && %letter !$= "0"))
         continue;
         
      if(%build !$= "-" && %build !$= "" && (%letter * 1 == 0 && %letter !$= "0"))
         break;
      
      if(%build $= "NULL")
         %build = "";
         
      %build = %build @ %letter;
   }
   
   return %build;
}


in before strreplace
« Last Edit: June 13, 2014, 11:16:02 PM by elm »

By 'not a thing' I meant 'the differentiation between 5 and 5.0 doesn't really matter'
I've had all of 1 case where the difference actually does matter, so I'll agree with you there.

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