Custom entity lua state handling in blueprints.

Things that already exist in the current mod API
Post Reply
User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Custom entity lua state handling in blueprints.

Post by Adil »

It is easy to come up with a modded entity, that may have some lua information assotiated to it, the recently discussed inserter with filters defined by network.
Sometimes, a certain state of such associated data may be required for the layout to work, for example a table of conditions of the above mentioned network inserter.

Thus it would be nice to have a set of events on_blueprint_created, on_blueprint_placed, on_blueprint_cleared which should provide the unique identifier of blueprint layout and the ability to inspect the entities used in layout creation, so that modders could handle the templating of their entities themselves. Or maybe other mechanism of storing the associated data in blueprints, so that it would be accessible in a special field of a ghost entity.
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.

Tinyboss
Fast Inserter
Fast Inserter
Posts: 205
Joined: Sun Nov 16, 2014 12:11 pm
Contact:

Re: Custom entity lua state handling in blueprints.

Post by Tinyboss »

Really great idea, and necessary if we want mods to be able to provide "first-class entities" in Factorio.

Maybe when a blueprint is created, on_blueprint_created could be called for each entitity in the blueprint, and the return value of that function should be a table (or nil, if nothing needs to be saved), which would be associated to that entity in the created blueprint. Then when the blueprint is placed, on_blueprint_placed would be called for each entity, with the stored table (if any) passed back as a parameter.

Actually this isn't exactly right because we place ghosts with the blueprint and we want to restore the data to the actual constructed entity, but the main idea is that each entity can decide for itself what data it wants to store in the blueprint and how to restore it upon construction.

GopherAtl
Fast Inserter
Fast Inserter
Posts: 177
Joined: Sat Jan 31, 2015 7:54 pm
Contact:

Re: Custom entity lua state handling in blueprints.

Post by GopherAtl »

just the events, with a read/write copy of the entities table of the blueprint, so it metadata can be added (in create), or applied to the ghosts (in place).

For on_robot_built events, you could add a field to the event containing the ghost, assuming this isn't already the case (don't remember the wiki saying anything about it but haven't actually checked, either)

Definitely a good idea, would provide a capability we will ultimately need one way or the other (copying custom settings and states on mod entities via blueprints) and my gut says it wouldn't be too much work on the dev's end.
My Mods:
Nixie Tubes - numeric displays for your circuit networks!
Logistic Combinators - use logistics values in circuit logic! -
Autowire - automate red/green wire connections

TheEdenCrazy
Manual Inserter
Manual Inserter
Posts: 3
Joined: Fri Jul 22, 2016 7:27 am
Contact:

Re: Custom entity lua state handling in blueprints.

Post by TheEdenCrazy »

My proposal for the addition of blueprint custom data would be like so:
  • Ghost entities have two new fields

    Code: Select all

    blueprint_id
    and

    Code: Select all

    blueprint_entity_id
  • Blueprint data can not only be accessed via the item, but created blueprints can be accessed through a

    Code: Select all

    game.blueprint_data
    This is a table mapping blueprint_ids to blueprint data (name, entities, etc) - see the LuaItemStack section of the Factorio Lua APIl
  • 3 new events, all with blueprint_id field:
    • Blueprint creation event: Called when a blueprint is created, this contains a field blueprint_entity_id_map, mapping blueprint_entity_ids to entities in the real world.
    • Blueprint placement event: Called when a blueprint is placed.
    • Blueprint destruction event: Called when a blueprint is deleted or modified.
  • When a blueprint is placed, the ghost images get blueprint_id assigned to the bluprint_id of the blueprint they were created from, and blueprint_entity_id is assigned to the blueprint_entity_id of the entity that it was based on when the blueprint was created.
The blueprint_id and blueprint_entity_id are there so that mods can store custom data about entities in blueprints in their global tables. Then in their on_*_built event handlers could check what ID and what blueprint the objects came from, so that they could load data that was set during the blueprint creation method into the new entities.


thelordodin
Fast Inserter
Fast Inserter
Posts: 148
Joined: Fri Jan 06, 2017 1:54 am
Contact:

Re: Custom entity lua state handling in blueprints.

Post by thelordodin »

+1 Up!

Workaround solution here: viewtopic.php?f=28&t=49709#p288672

thelordodin
Fast Inserter
Fast Inserter
Posts: 148
Joined: Fri Jan 06, 2017 1:54 am
Contact:

Re: Custom entity lua state handling in blueprints.

Post by thelordodin »

Will this ever be implemented?

Rseding91
Factorio Staff
Factorio Staff
Posts: 13218
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Custom entity lua state handling in blueprints.

Post by Rseding91 »

thelordodin wrote:Will this ever be implemented?
I don't know what's really being asked for. There are several things here that conflict with each other in utility and some that just aren't feasible.
If you want to get ahold of me I'm almost always on Discord.

thelordodin
Fast Inserter
Fast Inserter
Posts: 148
Joined: Fri Jan 06, 2017 1:54 am
Contact:

Re: Custom entity lua state handling in blueprints.

Post by thelordodin »

Rseding91 wrote:
thelordodin wrote:Will this ever be implemented?
I don't know what's really being asked for. There are several things here that conflict with each other in utility and some that just aren't feasible.
It's all about
It is easy to come up with a modded entity, that may have some lua information assotiated to it
A lua value field is needed on entities. This field must be preserved with the entity if it in blueprint or a ghost.

Literally I mean:
constant_combinator1.custom_data['mod1'] = ...

And custom_data is a lua table on every entity.

Rseding91
Factorio Staff
Factorio Staff
Posts: 13218
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Custom entity lua state handling in blueprints.

Post by Rseding91 »

I see. Unfortunately something like that isn't feasible as the performance and memory usage trade off would be too large.
If you want to get ahold of me I'm almost always on Discord.

thelordodin
Fast Inserter
Fast Inserter
Posts: 148
Joined: Fri Jan 06, 2017 1:54 am
Contact:

Re: Custom entity lua state handling in blueprints.

Post by thelordodin »

Lackily we now have an ability to save value in programmable speaker's alert message.
I just got this idea and tested if it's able to store 1 Mb string: yes It is!
And it's preserved in blueprints.

So now mods can really store any info in it.
I hope it wouldn't be limited in short while?

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Custom entity lua state handling in blueprints.

Post by eradicator »

Rseding91 wrote:I see. Unfortunately something like that isn't feasible as the performance and memory usage trade off would be too large.
How feasible would it be to have a callback when an entity is blueprinted, something like:
  1. An entity is added to a blueprint and the game raises on_entity_blueprinted, containing event.entity = (the entity blueprinted)
  2. A modder can register a function to on_entity_blueprinted like to any other event. But for this event the function can return a table (most likely something copied from the mods lua instance global table) that the modder wants to be saved to the blueprint for that entity.
  3. ???
  4. The player places a blueprint in the world.
  5. The game raises on_entity_built with an additional element event.mod_data which contains the table that was originally returned by the handler function and stored in the blueprint.
  6. The modder can do whatever he needs to the newly created ghost using the data from the table.
This should only have a performance impact if a mod actually registers a handler to on_entity_blueprinted right? I guess it might be supported by a prototype flag "requires_custom_blueprint_data" so on_entity_blueprinted doesn't need to be raised for tons of entities that don't need it. (Entities that could profit from this in my own mods are mostly things that even in a megafactory would only be required in small numbers (<100)).

[feasible/unfeasible] [might eventually happen/ain't gonna happen]?
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

slippycheeze
Filter Inserter
Filter Inserter
Posts: 587
Joined: Sun Jun 09, 2019 10:40 pm
Contact:

Re: Custom entity lua state handling in blueprints.

Post by slippycheeze »

I got annoyed by my AttachNotes data not carrying over, so I point you to this post with a library to save arbitrary and unlimited data in blueprints. It uses the msgpack binary format to encode (a limited subset of) data compactly with bit swizzling, and then stores it 32 bits at a time in a constant combinator with signals.

This allows a reasonably unlimited amount of data storage - it defines a 500 slot combinator, or 2000 bytes of data. msgpack overhead is pretty close to zero - 1-2 bytes for a single string - and this allows arbitrary tables, not just strings. Trivially extensible to unlimited data with the addition of a "sequence" and/or "entity" label in signals.

If you wanted to make regrettable life choices you could apply the same thing to simply encode a string in one-or-more combinators.

Also potentially applicable is the fact you can export an item stack entity (eg: blueprint, item-with-tags) as a blueprint string (ie: zlib compressed, base64 encoded) so you could use that to pack your arbitrary (or string) data prior to injecting it into the signal fields.

Post Reply

Return to “Already exists”