Making a composite entity act like a normal entity

Place to get help with not working mods / modding interface.
half a cat
Inserter
Inserter
Posts: 41
Joined: Sun Jul 16, 2023 4:03 pm
Contact:

Making a composite entity act like a normal entity

Post by half a cat »

I am making a composite entity and having trouble making it behave like a normal entity for everything that can create and destroy it in the engine. I would appreciate some guidance.

The composite entity comprises a power generator and a crafting machine, which are both assemblers. Players interact with the crafting machine, which is invisible. Players see the power generator and don't interact with it. The power generator can be active at different times than the crafting machine so there's meaningful player feedback in seeing it run.

Scripting composite entities involves letting the engine act on one entity, catching the event, and handling other entities with a script. The approaches I tried had problems:

* If the crafting entity is the main entity that's constructed and deconstructed, then it doesn't produce the expected UI feedback because it is invisible. When the player is about to build it, there is no preview image of the building.

* If the power generator is the main entity that's constructed and deconstructed, players can't deconstruct it because they can't interact after it is built.

* If the power generator is constructible and the crafting entity is deconstructible, then undo and cut-paste don't work. Presumably this is because the engine expects deconstruction and construction to be on the same entity.

How do I make this work?
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3738
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Making a composite entity act like a normal entity

Post by DaveMcW »

You need a third entity whose only purpose is to appear in blueprints.
robot256
Smart Inserter
Smart Inserter
Posts: 1140
Joined: Sun Mar 17, 2019 1:52 am
Contact:

Re: Making a composite entity act like a normal entity

Post by robot256 »

I've done composite entities every way you can imagine.

Using a third entity that is a copy of the crafting machine but with the power generator sprites is a good option. To make it work in blueprints, you have to handle the blueprint creation events and swap the entity name in your script.

In some cases, I actually make invisible the sub-entities blueprintable as well so all the settings are preserved. They show up as "required materials" but the sub-entity items aren't actually craftable. The script will revive the remaining sub-entity ghost when the main placeable entity is built. To make the ghosts behave properly, you also need to handle the events for when one sub-entity ghost is deleted and delete the other sub-entity in such a way that it is appended to the most recent undo queue item. This might be the best solution in your case.

Edit: Another option is to edit your sprites so that the interactable, blueprintable assembler has the "chassis" of the building, and the non-interactable, non-blueprintable generator has the "animation" of the building. You have to tweak the Y coordinate of the sub-entity to make sure the two pieces are drawn in the right order. Then you only have to make the assembler blueprintable, and have the script create/delete/clone/teleport the generator whenever the assembler is affected.
half a cat
Inserter
Inserter
Posts: 41
Joined: Sun Jul 16, 2023 4:03 pm
Contact:

Re: Making a composite entity act like a normal entity

Post by half a cat »

I followed that advice and made blueprints work by modifying BlueprintEntities in on_player_setup_blueprint, but it only works for the blueprint tool, not for cut or copy. Although cut and copy actions will produce a blueprint event, the event handler can't read player.blueprint_to_setup, which is what I used to modify a blueprint. What can I do here?

It also isn't behaving as expected for undo and redo. I haven't spent time on that yet.
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3738
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Making a composite entity act like a normal entity

Post by DaveMcW »

The event for cut and copy is on_player_cursor_stack_changed.
half a cat
Inserter
Inserter
Posts: 41
Joined: Sun Jul 16, 2023 4:03 pm
Contact:

Re: Making a composite entity act like a normal entity

Post by half a cat »

Thanks, I got cut and paste to work.

Undo still does not work. There are other messages that say it can't be done (viewtopic.php?p=602029#p602029) (viewtopic.php?p=427782#p427782) so I guess it is not possible to make custom entities undoable.
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3738
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Making a composite entity act like a normal entity

Post by DaveMcW »

robot256
Smart Inserter
Smart Inserter
Posts: 1140
Joined: Sun Mar 17, 2019 1:52 am
Contact:

Re: Making a composite entity act like a normal entity

Post by robot256 »

Also see LuaUndoRedoStack

I have it *mostly* working for some of my mods. When the main entitiy is mined, the game adds it to a mew undo stack level. My script handles the mining event and deletes the sub entities, and also adds the sub entities to the same undo stack entry as the main entity.
Post Reply

Return to “Modding help”