Run a function anytime a brick is planted

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

Is there a way i can run a function anytime a brick is planted to check if it is a certain brick?

This kinda stuff could be easily found out with a trace, or just looking at someone else's code.
CityRPG does this.

You can parent serverCmdPlantBrick and then check what the %client.player.tempBrick is.

the methods you want are fxDTSBrickData::onPlant(%this, %obj) and fxDTSBrickData::onLoadPlant(%this, %obj)

%this is the brick datablock
%obj is the brick that was planted

onLoadPlant is called specifically when a brick is loaded, onPlant is called in all other cases

you can replace fxDTSBrickData with your own brick datablock to add datablock-specific functionality, e.g. brick1x4fData::onPlant. ofc, it probably goes without saying, but you should package fxDTSBrickData methods.

This kinda stuff could be easily found out with a trace, or just looking at someone else's code.
CityRPG does this.

You can parent serverCmdPlantBrick and then check what the %client.player.tempBrick is.
thanks for a new question, How do you use traces?


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.
« Last Edit: July 22, 2015, 03:51:56 AM by Val »

You can parent serverCmdPlantBrick and then check what the %client.player.tempBrick is.
the methods you want are fxDTSBrickData::onPlant(%this, %obj) and fxDTSBrickData::onLoadPlant(%this, %obj).

You'll want one of these, though which one depends on specifically what you want it to do. If you say exactly what you want to do, we could tell your which is better

You'll want one of these, though which one depends on specifically what you want it to do. If you say exactly what you want to do, we could tell your which is better
ok, im looking to do something on the lines of If you try and place a tire it will remove 10 rubber. but if it is a wooden wall it will remove 10 wood

ok, im looking to do something on the lines of If you try and place a tire it will remove 10 rubber. but if it is a wooden wall it will remove 10 wood
serverCmdPlantBrick is probably the best

serverCmdPlant is probably the best
serverCmdPlant doesn't guarantee that anything was actually planted

yeah would probs be best actually nvm

The method Val linked would work best for that.

you also want to stop players from planting bricks if they don't have enough resources, and serverCmdPlant would probably be the better option for that

ofc since it doesn't guarantee that anything was planted you'd want something like what val posted to actually deduct resources ya

Yeah, if you're going to set variables to bricks serverCmdPlanBrick isn't your thing to use

Couldn't you parent plantbrick and just like

get client's current brick group size
Call parent
get brick group size again
If size is greater than previous size, then assume it was successfully planted?


The problem with using onPlant to prevent bricks from being planted is that you're left with a click sound from placing, destroy sound, and falling killed brick, which kinda looks/sounds bad and some people will use it to spam sounds by holding down plant brick

Couldn't you parent plantbrick and just like

get client's current brick group size
Call parent
get brick group size again
If size is greater than previous size, then assume it was successfully planted?
assuming everything happens immediately, yeah

would have to do some testing to see if that's the case