Game Development Community

Typecasting or Integer Division?

by Justin Wright · in Technical Issues · 08/11/2012 (7:09 pm) · 2 replies

Is there any way to do integer division in torquescript? Or typecasting? Basically I just need the integer part of a division operation.

#1
08/11/2012 (8:45 pm)
You can use mFloor() or mCeil() to round a number up or down, respectively. If you just want to truncate the number (essentially removing the numbers after the decimal without rounding) you can use this bit of script from Michael Hall (thread: http://www.garagegames.com/community/forums/viewthread/125974):

function cropDecimal(%num)
{
  %length = strlen(%num);
  %dot = 0;

   for(%i = 0; %i < %length; %i++)
   {
      if(getSubStr(%num, %i, 1) $= ".")
         %dot = %i;
   }

   if(%dot > 0)
     %name = getSubStr(%num, 0, %dot);
   else
      %name = %num;

  return %name;
}

The best (most performant) thing to do, provided you have access to the engine source code, would be to add a new ConsoleFunction to perform the truncation.
#2
08/11/2012 (9:05 pm)
Thanks. Exactly what I needed.