Page 1 of 1

Allow on_pre events to return false and abort the event

Posted: Sun May 21, 2017 6:12 pm
by sparr
I would like to be able to return false (or some other sentinel value) from my event handler for on_pre_* events and cause the rest of the event to not happen. This would work like browser javascript event handlers that disable the default functionality for the event that is being triggered.

Re: Allow on_pre events to return false and abort the event

Posted: Sun May 21, 2017 6:15 pm
by Ranakastrasz
On that thought, I would like to be able to have you change any of the arguments the event handler holds, and have it use those instead.

Re: Allow on_pre events to return false and abort the event

Posted: Sun May 21, 2017 10:37 pm
by Rseding91
Factorio events don't support this concept. In fact the game itself doesn't support this concept.

So, this isn't likely to ever happen.

Re: Allow on_pre events to return false and abort the event

Posted: Thu May 25, 2017 2:27 am
by SilverB1rd
Image

When trying to build a new entity in a location that is not valid, like a furnace on top of belts, the game stops the player from building the object. I don't know if this concept really applies to on_pre or on_put_item events. However something like on_validate_entity_placement event where placement could be denied before the game starts the on_put_item -> on_built_entity event chain would be very useful for scenarios I'm currently working on.

I work around this right now by deleting the entity in on_built_entity and refunding the item to the player's inventory, which is not really intuitive.

Re: Allow on_pre events to return false and abort the event

Posted: Thu Jun 15, 2017 4:04 pm
by mickael9
In case you didn't know, you can cancel an item placement by doing player.clean_cursor() in on_put_item (before on_built_entity gets raised). Of course the player won't have the item in hand anymore after this. Similarly, you can change what is being placed by changing the player's cursor_stack in on_put_item.

Re: Allow on_pre events to return false and abort the event

Posted: Fri Jun 23, 2017 7:15 am
by doc
mickael9 wrote:In case you didn't know, you can cancel an item placement by doing player.clean_cursor() in on_put_item (before on_built_entity gets raised). Of course the player won't have the item in hand anymore after this. Similarly, you can change what is being placed by changing the player's cursor_stack in on_put_item.
What happens if the player has no inventory space available?

Re: Allow on_pre events to return false and abort the event

Posted: Fri Jun 23, 2017 10:39 pm
by Mooncat
doc wrote:
mickael9 wrote:In case you didn't know, you can cancel an item placement by doing player.clean_cursor() in on_put_item (before on_built_entity gets raised). Of course the player won't have the item in hand anymore after this. Similarly, you can change what is being placed by changing the player's cursor_stack in on_put_item.
What happens if the player has no inventory space available?
Either drop the item on ground or put it back to the cursor in the next tick and hopefully no one will notice?

Re: Allow on_pre events to return false and abort the event

Posted: Tue Jun 27, 2017 3:54 pm
by darkfrei
Mooncat wrote:Either drop the item on ground or put it back to the cursor in the next tick and hopefully no one will notice?
You can place and remove on the same tick, but you can get dust of placing.