with the new item-with-tags type, we can add data to items. Now I have a few questions/suggestions. In simple scenario we have a item-with-tag and a simple-entity-with-owner.
Code: Select all
data:extend{{
type = "item-with-tags",
name = "ght-item-with-tags",
place_result = "ght-simple-entity-with-owner",
-- rest omitted
}, {
type = "simple-entity-with-owner",
name = "ght-simple-entity-with-owner",
minable = { result = "ght-item-with-tags" },
-- rest omitted
}}
Code: Select all
local function on_mined_entity(event)
local robot = event.name == defines.events.on_robot_mined_entity
local entity = event.entity
local who = event.player_index and game.players[event.player_index] or entity.force
local buffer = event.buffer
local print = who.print
local printf = function(...) print(string.format(...)) end
printf("mined%s %s at (%d,%d)", robot and " by robot" or "", entity.name, entity.position.x, entity.position.y)
for i = 1, #buffer do
local stack = buffer[i]
printf("stack: %s (%d)", stack.name, stack.count)
if stack.name == "ght-item-with-tags" then
stack.set_tag("pos-x", entity.position.x)
stack.set_tag("pos-y", entity.position.y)
end
end
end
script.on_event(defines.events.on_player_mined_entity, on_mined_entity)
script.on_event(defines.events.on_robot_mined_entity, on_mined_entity)
Code: Select all
local function on_built_entity(event)
local robot = event.name == defines.events.on_robot_built_entity
local entity = event.created_entity
local who = event.players_index and game.players[event.player_index] or entity.force
local item = event.item
local tags = event.tags
local print = who.print
local printf = function(...) print(string.format(...)) end
printf("build%s %s at (%d,%d)", robot and " by robot" or "", entity.name, entity.position.x, entity.position.y)
printf("item: %s", item or "[unknown]")
printf("tags: %s", serpent.dump(tags))
if item == "ght-item-with-tags" and tags then
local pos_x = tags["pos-x"]
local pos_y = tags["pos-y"]
if pos_x and pos_y then
pos_x = pos_x - entity.position.x
pos_y = pos_y - entity.position.y
local dist = math.sqrt(pos_x * pos_x + pos_y * pos_y)
printf("item moved %.1f tiles", dist)
end
end
end
script.on_event(defines.events.on_built_entity, on_built_entity)
script.on_event(defines.events.on_robot_built_entity, on_built_entity)
- if I create a blueprint, I can't store any data to it
- if I place a blueprint, the on_robot_built_entity has no event.item
- if I place a blueprint, the robots uses items, which has tags, but those tags are "lost" after placing the item
I suggest the following extension to the modding interface:
An on_marked_for_blueprint event, with the following information:
- player_index: the player, who marked the item for a blueprint
- entity: the entity to be blueprinted
- item: the name of the item, which will be stored in the blueprint -- can be changed by mod
- tags: if item is a item-with-tags, then the table with tags
- config: additional config for the entity, can be used for customization by mods
The LuaEntity object gets the following additional properties:
- ghost_item: the item used to build the entity
- ghost_tags: the tags associated with item to built this entity
- ghost_config: the configuration stored for this entity
- item: the item used to build this entity
- tags: the tags associated with this item
- config: the configuration stored for this entity
To explain the difference between tags and config:
- tags is to "permanetly" bind an information to an item/entity. e.g. if a "clean" object is modified by a process to be better/larger/faster, then this information should be preserved. Say you have an inserter and enlarge its reach with a recipe, then you can store a tag to the inserter.
- config is to save/restore a configuration of an entity. E.g. bobs inserters can grab and place in various directions, this should be saved in the config for the blueprint. The mod, which stores this information can later make the necessary steps to restore its orignal behavior. But it is not a permanent information of this inserter. Its more like a recipe or direction.