Author Topic: OnPlantBrick Help  (Read 1393 times)

I want to trigger some code whenever/before a user places down a brick.
I have tried search, and I tried looking through other add-ons for this. But I cannot find it.

I am new-ish to Torque Script and I am still getting the hang of things.

What I want to do is check some variables before a user places down a brick.
For example make it cost /resources/ or something to place bricks.

Also if there's some kind of formula to help vary the cost of different kinds of bricks, that'd be great too. But that's just a bonus.
========================
Since I posted this I have learned it's

function servercmdplantbrick(%cl)

==Idea==
package AdatPlantBrick
{
    function serverCmdPlantBrick(%client)
    {
        if(%Client.Credits > $BrickCost)
        {
            %Client.Credits-=$BrickCost;
            $Treasury+=$BrickCost;
            return Parent::ServerCmdPlantBrick(%Client);
        }
        else
        {
            CenterPrint(%client,'',"\c6You do not have enough credits!",1);
        }
    }
};
activatePackage(AdatPlantBrick);
« Last Edit: October 18, 2013, 03:49:09 PM by Aduioa »

fxDTSBrick::onLoadPlant(%this)
fxDTSBrick::onPlant(%this)

fxDTSBrick::onLoadPlant(%this)
fxDTSBrick::onPlant(%this)
What is %this?

Also I tried my code above, and it makes the ghost brick disappear, and doesnt place the brick.. The variables work though.
« Last Edit: October 18, 2013, 03:50:56 PM by Aduioa »


Instead of using if(%Client.Credits > $BrickCost) I suggest doing if(%Client.Credits >= $BrickCost) so that if they have exactly enough credits, they can plant the brick. As it is now, they need more than enough credits.

For the cost I suggest doing $brickCost = mFloor(%brick.getDataBlock().getVolume() + 0.5); This will make the cost the volume of the brick, and then if it's not a whole number, it rounds it to the nearest whole number.

Quote
9:56 PM - Port✿: so what exactly is the problem here
9:56 PM - Auios: when I try to plant a brick it removes the ghost brick and doesnt plant
9:56 PM - Auios: even if the conditions are true
9:57 PM - Auios: but the variables work
9:57 PM - Auios: lets pretend brickcost = 5
9:57 PM - Auios: so I have 100
9:57 PM - Auios: and I try to plant the brick
9:57 PM - Auios: i will have 95
9:57 PM - Auios: but no brick
9:57 PM - Auios: ghostbrick kills itself too

For the cost I suggest doing $brickCost = mFloor(%brick.getDataBlock().getVolume() + 0.5); This will make the cost the volume of the brick, and then if it's not a whole number, it rounds it to the nearest whole number.

He's already calculating it in a completely different way: 9:56 PM - Auios: $BrickCost = $AverageWealth * .01;

He's already calculating it in a completely different way: 9:56 PM - Auios: $BrickCost = $AverageWealth * .01;
I've changed it since then. It's now $BrickCost = ($AverageCredits / 90) + 5;

For the cost I suggest doing $brickCost = mFloor(%brick.getDataBlock().getVolume() + 0.5); This will make the cost the volume of the brick, and then if it's not a whole number, it rounds it to the nearest whole number.
Ah, that's great thanks! I'll see if I can use that.
« Last Edit: October 18, 2013, 04:20:30 PM by Aduioa »

For a variable cost on bricks you could calculate it by getting the brickSizeX brickSizeY and brickSizeZ of the brick's datablock

I would like to point out here that fxDtsBrickData::getVolume(%data) is NOT a default function.

Code: [Select]
function fxDtsBrickData::getVolume(%data)
{ return %data.brickSizeX * %data.brickSizeY * %data.brickSizeZ; }

function fxDtsBrick::getVolume(%brick)
{ return %brick.getDatablock().getVolume(); }

I decided not to use volume to help decide the cost of bricks. But I still have unsolved problems. Like the fact that I don't have to place down a brick and it still takes money. Basically I can just hold enter and drain my money.

function fxDTSBrick::onPlant(%brick)
{
   // Stating a variable for returning the
   // parent so nothing is messed up when
   // adding this code onto onPlant.

   %r = Parent::onPlant(%brick);

   // Get the client from the brick.
   %client = %brick.getGroup().client;
      
   // Check if the client's Credits are
   // NOT greater than the required cost.
   // If they aren't then the client shouldn't
   // be able to plant the brick!

   if(%client.Credits < $BrickCost)
   {
      // Since the client doesn't meet the
      // requirements to plant the brick,
      // message the client that he couldn't
      // place the brick, delete the brick
      // and return/exit the function.

      commandToClient(%client, 'centerPrint', "Insufficient Credits!", 3);
      %brick.schedule(0, "delete");
      return %r;
   }

   // Your stuff that you want to happen
   // when the brick is properly planted.

   %client.Credits -= $BrickCost;
   $Treasury += $BrickCost;

   // Instead of returning %r you could
   // just return 'Parent::onPlant(%brick)'
   // but since they both return the same
   // thing (%r being declared above), it
   // doesn't matter. I personally find using
   // 'return %r' to be more organized and
   // easier. It's just a choice of preference.
   // =========================================
   // Exit the function, allowing the default
   // Blockland onPlant code and other code
   // from other packages to execute.

   return %r;
}
« Last Edit: October 24, 2013, 09:05:13 AM by Vaux »