Saving entity custom data to blueprint

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

Saving entity custom data to blueprint

Post by thelordodin »

I'm wtiring a ComplexCombinator mod (viewtopic.php?f=97&t=49691). This entity have many settings which is set with custom gui.
I bind this data to the entity by position key, this way I can read and write it inside the game, but, it can't handle blueprints...

So, is there a way to save some custom data with my entity into blueprint?

If there is no way I suggest adding a lua table property "mod_data" to LuaEntity class.

(No answer on "Modding help" forum, so I post it here...)
User avatar
Mooncat
Smart Inserter
Smart Inserter
Posts: 1196
Joined: Wed May 18, 2016 4:55 pm
Contact:

Re: Saving entity custom data to blueprint

Post by Mooncat »

You can create a combinator that can actually stores circuit signals, so your custom data can be stored in blueprint in the form of signals. But you will need to convert the signals from and to your data. It is not easy.

+1 for the request for adding custom entity data support in blueprint.

The request was also made long time ago: viewtopic.php?f=28&t=14898&p=213571

Hopefully we can set custom table in entity, and such table can be stored in blueprint.
(My 1000th post. :P )
thelordodin
Fast Inserter
Fast Inserter
Posts: 153
Joined: Fri Jan 06, 2017 1:54 am
Contact:

Re: Saving entity custom data to blueprint

Post by thelordodin »

Mooncat wrote:You can create a combinator that can actually stores circuit signals, so your custom data can be stored in blueprint in the form of signals. But you will need to convert the signals from and to your data. It is not easy.
Have anyone done this before me? Do you know of any mods implementing this?
User avatar
Mooncat
Smart Inserter
Smart Inserter
Posts: 1196
Joined: Wed May 18, 2016 4:55 pm
Contact:

Re: Saving entity custom data to blueprint

Post by Mooncat »

thelordodin wrote:
Mooncat wrote:You can create a combinator that can actually stores circuit signals, so your custom data can be stored in blueprint in the form of signals. But you will need to convert the signals from and to your data. It is not easy.
Have anyone done this before me? Do you know of any mods implementing this?
https://mods.factorio.com/mods/NiftyManiac/StickyNotes
https://mods.factorio.com/mods/Earendel/textplates
thelordodin
Fast Inserter
Fast Inserter
Posts: 153
Joined: Fri Jan 06, 2017 1:54 am
Contact:

Re: Saving entity custom data to blueprint

Post by thelordodin »

Mooncat, thank you very much !

I've made a library for this problem. Haven't runned it yet (i can't run factorio now), so its untested for the moment.

--------------------------------------------------------------------------------------------------------------------------------
-- Author: thelordodin
-- Special thanks to Mooncat - who guided me how to do this.
-- License: free to copy, change, and use in any projects. No warranty.
--------------------------------------------------------------------------------------------------------------------------------
This library allows you to save custom data to combinator entiry.
Custom data must be in lua table format.

Data is stored as values of signals in combinator.

To save data to combinator use:
write_to_combinator(combinator, data)

To load:
data = read_from_combinator(combinator)

This library uses MessagePack to pack the table, so it'll be stored in quite compact format, hovewer
be warned that data size is still very limited and is determinated by item_slot_count parameter of combinator.

Not deeply tested lib, but works somehow.
Attachments
blueprint_custom_data.zip
(7.14 KiB) Downloaded 239 times
thelordodin
Fast Inserter
Fast Inserter
Posts: 153
Joined: Fri Jan 06, 2017 1:54 am
Contact:

Re: Saving entity custom data to blueprint

Post by thelordodin »

After testing I've changed lib in previous post to working one.
User avatar
micromario
Fast Inserter
Fast Inserter
Posts: 109
Joined: Thu Apr 05, 2018 11:53 am
Contact:

Re: Saving entity custom data to blueprint

Post by micromario »

Hello. I don't know when this was added but blueprint ghosts now support the "tags" attribute for every entity.
This means you can save data to blueprints without any hidden combinators/programmable speakers

Here is a quick example on how to use the feature

Saving data

Code: Select all

local function save_blueprint_data(blueprint, mapping)
    for i, entity in pairs(mapping) do
        if entity.valid then
            local requester_data = global.requesters[entity.unit_number]
            if requester_data then
                blueprint.set_blueprint_entity_tag(i, 'requested_item', requester_data.requested_item)
                blueprint.set_blueprint_entity_tag(i, 'request_size', requester_data.request_size)
            end
        end
    end
end

-- saving to copy-paste tool & cut-paste tool
script.on_event(defines.events.on_player_setup_blueprint, function(event)
    local player = game.players[event.player_index]
    
    local cursor = player.cursor_stack
    if cursor and cursor.valid_for_read and cursor.type == 'blueprint' then
        save_blueprint_data(cursor, event.mapping.get())
    else
        global.blueprint_mappings[player.index] = event.mapping.get()
    end
end)

-- saving to regular blueprint
script.on_event(defines.events.on_player_configured_blueprint, function(event)
    local player = game.players[event.player_index]
    local mapping = global.blueprint_mappings[player.index]
    local cursor = player.cursor_stack
    
    if cursor and cursor.valid_for_read and cursor.type == 'blueprint' and mapping and #mapping == cursor.get_blueprint_entity_count() then
        save_blueprint_data(cursor, mapping)
    end
    global.blueprint_mappings[player.index] = nil
end)
Loading data

Code: Select all

local function on_built(event)
    local tags = event.tags
    global.requesters[requester.unit_number] = {
        entity = requester,
        requested_item = tags and tags.requested_item or nil,
        request_size = tags and tags.request_size or 0
    }
end
The only drawback to this method is that if the player clicks this button
Image
Then the data will not be copied

This is because the blueprint item does not have an internal reference to the original blueprinted entities
Post Reply

Return to “Implemented mod requests”