Author Topic: Script_BrickRange - only works while debugging >.O  (Read 1825 times)

So this add-on is supposed to make it so you can only build within a certain range of the spawn point.

I have 2 versions of the script.  One that doesn't work. And another one that works perfectly while in debug mode, and half-works while out of debug mode(just because it has the capability to go into debug mode).

I'm sure I'm just missing some syntax somewhere, but I can't seem to figure it out.

This is the one with debuggingness.
Code: (Script_BrickRange\server.cs - debug) [Select]
package BrickRangePackage
{
function fxDTSbrick::Plant(%brick)
{
%e = Parent::Plant(%brick);
if(!%e)
{
%c = getbrickgroupfromobject(%brick).client;
if(isObject(%c) && isObject(%c.player) && isObject(%c.player.tempbrick))
{
%pos1x = getWord(%c.player.tempbrick.getWorldBox(), 0);
%pos1y = getWord(%c.player.tempbrick.getWorldBox(), 1);
%pos2x = getWord(%c.player.tempbrick.getWorldBox(), 3);
%pos2y = getWord(%c.player.tempbrick.getWorldBox(), 4);
if(%pos1x > 124 || %pos1x < -124 || %pos1y > 124 || %pos1y < -124 || %pos2x > 124 || %pos2x < -124 || %pos2y > 124 || %pos2y < -124)
{
commandtoclient(%c,'centerprint',"\c6Too far - Build closer to the center.",2);
%brick.schedule(0, "delete");
if($debug)
{
echo("Return is true.");
}
}
if($debug)
{
echo(%c SPC %brick SPC %pos1x SPC %pos1y SPC %pos2x SPC %pos2y);
}
}
}
}
};
ActivatePackage(BrickRangePackage);

This is the one without.
Code: (Script_BrickRange\server.cs) [Select]
package BrickRangePackage
{
function fxDTSbrick::Plant(%brick)
{
%e = Parent::Plant(%brick);
if(!%e)
{
%c = getbrickgroupfromobject(%brick).client;
if(isObject(%c) && isObject(%c.player) && isObject(%c.player.tempbrick))
{
%pos1x = getWord(%c.player.tempbrick.getWorldBox(), 0);
%pos1y = getWord(%c.player.tempbrick.getWorldBox(), 1);
%pos2x = getWord(%c.player.tempbrick.getWorldBox(), 3);
%pos2y = getWord(%c.player.tempbrick.getWorldBox(), 4);
if(%pos1x > 124 || %pos1x < -124 || %pos1y > 124 || %pos1y < -124 || %pos2x > 124 || %pos2x < -124 || %pos2y > 124 || %pos2y < -124)
{
commandtoclient(%c,'centerprint',"\c6Too far - Build closer to the center.",2);
%brick.schedule(0, "delete");
}
}
}
}
};
ActivatePackage(BrickRangePackage);
« Last Edit: July 02, 2013, 09:23:32 PM by Dglider »

Not that I have tested this, but I would do it this way:
Code: [Select]
package BrickRangePackage
{
function fxDTSbrick::Plant(%brick)
{
%c = getbrickgroupfromobject(%brick).client;
if(isObject(%c) && isObject(%c.player) && isObject(%c.player.tempbrick))
{
%pos1x = getWord(%c.player.tempbrick.getWorldBox(), 0);
%pos1y = getWord(%c.player.tempbrick.getWorldBox(), 1);
%pos2x = getWord(%c.player.tempbrick.getWorldBox(), 3);
%pos2y = getWord(%c.player.tempbrick.getWorldBox(), 4);
if(%pos1x > 124 || %pos1x < -124 || %pos1y > 124 || %pos1y < -124 || %pos2x > 124 || %pos2x < -124 || %pos2y > 124 || %pos2y < -124)
{
commandtoclient(%c,'centerprint',"\c6Too far - Build closer to the center.",2);
return false;
}
}
}
parent::Plant(%brick);
};
ActivatePackage(BrickRangePackage);
EDIT: FIXED
« Last Edit: July 02, 2013, 11:40:36 PM by Honorabl3 »

I had to flip it a little but it works ^.^

Code: [Select]
package BrickRangePackage
{
function fxDTSbrick::Plant(%brick)
{
%e = Parent::Plant(%brick);
if(!%e)
{
%c = getbrickgroupfromobject(%brick).client;
if(isObject(%c) && isObject(%c.player) && isObject(%c.player.tempbrick))
{
%pos1x = getWord(%c.player.tempbrick.getWorldBox(), 0);
%pos1y = getWord(%c.player.tempbrick.getWorldBox(), 1);
%pos2x = getWord(%c.player.tempbrick.getWorldBox(), 3);
%pos2y = getWord(%c.player.tempbrick.getWorldBox(), 4);
if(%pos1x > 124 || %pos1x < -124 || %pos1y > 124 || %pos1y < -124 || %pos2x > 124 || %pos2x < -124 || %pos2y > 124 || %pos2y < -124)
{
commandtoclient(%c,'centerprint',"\c6Too far - Build closer to the center.",2);
return true;
}
else
{
return false;
}
}
}
}
};
ActivatePackage(BrickRangePackage);

Can you explain to me why it works?  Like... why I need those returns there and and what they're doing?  It'd help alot for future projects.

I googled returns earlier and, I understand what they're for, but not why I would need one in this particular situation.

Returns stops the script from proceeding.

Well if my memory about plant errors doesn't fail me then that'll give you an "Overlap" error if you're too far from the center.
Maybe try to find one that better suits what you want? I think 4 is "out of range"...

Code: [Select]
...
{
   commandToClient(%c, 'centerprint', "\c6Too far - Build closer to the center.", 2);
   return 4;
}
...

Why this?

%pos1x = getWord(%c.player.tempbrick.getWorldBox(), 0);
%pos1y = getWord(%c.player.tempbrick.getWorldBox(), 1);
%pos2x = getWord(%c.player.tempbrick.getWorldBox(), 3);
%pos2y = getWord(%c.player.tempbrick.getWorldBox(), 4);
if(%pos1x > 124 || %pos1x < -124 || %pos1y > 124 || %pos1y < -124 || %pos2x > 124 || %pos2x < -124 || %pos2y > 124 || %pos2y < -124)


Can't you just do something like this?

if(vectorLen(%c.player.tempBrick.position) >= max range goes here)

That would be a sphere as opposed to a square and would also allow large bricks to extend far beyond the bounds that other bricks could reach.

Still, at least don't get the brick's world box 4 times. Save it to a variable and get the individual words of it.

True. Plus the (I think first) group of coords is always the lower one, so he could do something like

Code: [Select]
...
%box = %brick.getWorldBox();
...
if(%lowX < -124 || %lowY < -124 || %highX > 124 || %highY > 124)
{
   ...
}
...

...


Four would be the "Unstable" plant error, "Out of Range" is six.

Four would be the "Unstable" plant error, "Out of Range" is six.
I don't think unstable is called anymore since the removal of terrain and interiors.

I don't think unstable is called anymore since the removal of terrain and interiors.

But the 'MsgPlantError_Unstable' message callback still exists.

Quote
messageClient(findClientByName("Your Handle"), 'MsgPlantError_Unstable');

I don't think unstable is called anymore since the removal of terrain and interiors.
It is if you plant bricks half over the edge of a staticshape

Well if my memory about plant errors doesn't fail me then that'll give you an "Overlap" error if you're too far from the center.
Maybe try to find one that better suits what you want? I think 4 is "out of range"...
Well I couldn't find Out of Range in any of numbers 1-6.  So I ended up just using TooFar.  It makes sense to use that one.

Still, at least don't get the brick's world box 4 times. Save it to a variable and get the individual words of it.
Done.

True. Plus the (I think first) group of coords is always the lower one, so he could do something like
I tested it out and that is the case.  Added.



Why this?

%pos1x = getWord(%c.player.tempbrick.getWorldBox(), 0);
%pos1y = getWord(%c.player.tempbrick.getWorldBox(), 1);
%pos2x = getWord(%c.player.tempbrick.getWorldBox(), 3);
%pos2y = getWord(%c.player.tempbrick.getWorldBox(), 4);
if(%pos1x > 124 || %pos1x < -124 || %pos1y > 124 || %pos1y < -124 || %pos2x > 124 || %pos2x < -124 || %pos2y > 124 || %pos2y < -124)


Can't you just do something like this?

if(vectorLen(%c.player.tempBrick.position) >= max range goes here)

Hmm yes.  I'd have to test all four top corners to keep
would also allow large bricks to extend far beyond the bounds that other bricks could reach.
this from happening but that's actually extremely useful.

If I drop the Z value I can turn it into a cylinder.


Anyhow here's what I have now.  Thanks for the help! ^.^
Code: (server.cs) [Select]
package BrickRangePackage
{
function fxDTSbrick::Plant(%brick)
{
%e = Parent::Plant(%brick);
if(!%e)
{
%c = getbrickgroupfromobject(%brick).client;
if(isObject(%c) && isObject(%c.player) && isObject(%c.player.tempbrick))
{
%worldbox = %c.player.tempbrick.getWorldBox();
%xLow = getWord(%worldbox, 0);
%yLow = getWord(%worldbox, 1);
%xHigh = getWord(%worldbox, 3);
%yHigh = getWord(%worldbox, 4);
if(%xLow < -124 || %yLow < -124 || %xHigh > 124 || %yHigh > 124)
{
messageClient(%c, 'MsgPlantError_TooFar');
return 7;
}
else
{
return false;
}
}
}
}
};
ActivatePackage(BrickRangePackage);