Page 1 of 1

[Solved]Intercepting creation of an entity from on_put_item?

Posted: Sat May 06, 2017 5:36 pm
by Pikaro
Hi!

I've been creating a mod to show direction indicators on belt placement since I was constantly screwing up the placement. For this, I created a "belt placer" item that is in the transport_belt fast_replaceable_group. On being built, it is replaced with a belt of the appropriate type and direction and added back to the player's inventory again. However, I have two problems:

a) The item can be built even with no belts in inventory. I could stop the process in on_built_entity and delete the entity instead of replacing it with a belt. But that's ugly and the sound for "thing built" still plays.

b) I get into an infinite build loop - the entity is created, replaced, and replaced with the placeholder entity again since it's in the fast_replaceable_group. This causes build sounds to play rapidly like when placing a lot of robots manually.

There are two direct ways around this I can think of: Intercepting the build and disabling the sound temporarily and play it back just once manually. Is either of those possible? For the former, I'd also need a way to get the direction the entity would be placed in, in order to emulate default belt placement behavior where nothing happens if you're building a belt in the same direction again. Is that possible, if this is a feasible idea? The latter would probably be quite performance-hungry, would it not?

If you have any other ideas or would suggest changing the concept, go ahead, this is my first mod and I'm having a hard time understanding how everything ties together.

Thanks!

Re: Intercepting creation of an entity from on_put_item?

Posted: Sun May 07, 2017 7:59 am
by Pikaro
Solved by partially changing the concept. To whom it may concern: You can actually call clean_cursor and cursor_stack.set_stack from on_put_item. Process for me:

In on_put_item:

1. Note down what tool was used and set a flag that it's in use
2. Clean the cursor
3. Set the cursor to the item you'd like to place
4. Clear the item from the inventory

In on_tick:

1. Check if the "in use flag" is set
2. Clear it and clean the cursor
3. Set the cursor to the noted tool
4. Clear the tool from the inventory

You could also do that in on_built_entity, but that doesn't trigger at all necessary times for me.

This behaves almost exactly as if you had tried to build with the intended result in the first place (i. e. no duplicate builds when re-building belt in the same direction etc) but shows the graphic for the tool item which in my case has arrows on them. Maybe it helps someone.