So this project was not intended to be a project but it is now. I've already spent several days longer on this than I initially planned. Oh well I'm learning bits and pieces of TorqueScript and I already have plans for expanding this for the future.
By Port's suggestions I've split the actual script into it's own function which I'm calling from the onFire method(or function?). The code is much cleaner now. Here's some code that actually works.
function Player::updateBrickDetector(%this)
{
cancel(%this.updateBrickDetector);
%name="_asd";
%radius=32;
initContainerRadiusSearch(%pos, %radius, $TypeMasks::FxBrickAlwaysObjectType);
while (isObject(%obj = containerSearchNext()))
{
if (%obj.getName() $= %name)
{
%pos=%this.getPosition();
%distance=mFloatLength(vectorDist(%pos, %obj.getPosition()),0);
%detectorMsg="\c6Detecting: \c3" @ getSubStr(%name, 1, strLen(%name)) @ "\c6 | Distance: \c3" @ %distance @ " Bricks";
commandToClient(%this.client,'bottomPrint',%detectorMsg,0,0);
break;
}
}
if(%this.getMountedImage(0) == nameToID(brickDetector_ON_Image) && isObject(%this) && (%this.getState() !$= "Dead"))
{
%this.updateBrickDetector = %this.schedule(1000, updateBrickDetector);
}
else
{
return;
}
}
Problems:
The brick is tracked way beyond the container search radius. Some black magic right there.
It is not tracking the closest brick to the player. I think %obj.getPosition() is not updating correctly. For some bizarre reason it only detects the oldest placed named brick. If that is destroyed it starts tracking the second oldest brick with the correct name regardless of player position.