Author Topic: Run a function anytime a brick is planted  (Read 2412 times)

Or just use plantBrick to check if there are resources and the trust check method val posted to actually deduct them and set brick properties.

You can't really set brick properties with PlantBrick, you could but it's not recommended.

You could use both:
onPlant/onLoadPlant: Check the datablock and then set brick values
PlantBrick: Return if not enough of whatever it has to check based on the temp's datablock

Or this.

Regarding the first question, here's a fail-safe alternative that covers trust and plant errors and has a function that will clean up your brick.

You can get the client by doing %obj.client, but keep in mind that there's cases where this field isn't set, so it's a good idea to check if the client exists before doing any client-specific operations.

The duplicator makes this slightly difficult to work with, though. onTrustCheckFinished will catch the bricks being planted by it, though it's not a good idea to remove them with the aforementioned %obj.TrustCheckFailed() because it will definitely screw up the client's undo stack. In that case just call %obj.schedule(0, delete); (schedule because the duplicator will do more operations after onTrustCheckFinished and deleting it directly will cause problems). This requires hooking duplicator-related functions in order to know if the brick comes from the duplicator. Try packaging createFromStr(%str, %client) and setting a global variable before and after to determine that.

Edit: createFromStr() also returns the brick the duplicator just created (and error codes otherwise), so it's better to just package the function and get the brick from what the parent returns, then do specific operations afterwards. Delete and return 0 for failure.

I think that is more complicated for the OP

I think that is more complicated for the OP
It's actually pretty simple.
You just package onTrustCheckFinished so that you know the brick is properly placed with the correct values instead of packaging another function where you don't know that this is true.