I'm trying to add loot (Small Alien Artifacts) to a table each time they are created.
I also need to remove them if they are picked up or somehow destroyed.
I'm trying the following, but it does not appear that they are "Built" when they are created:
Events:
Code: Select all
local build_events = {defines.events.on_built_entity, defines.events.on_robot_built_entity}
script.on_event(build_events, On_Built)
local pre_remove_events = {defines.events.on_pre_player_mined_item, defines.events.on_robot_pre_mined}
script.on_event(pre_remove_events, On_Remove)
local death_events = {defines.events.on_entity_died}
script.on_event(death_events, On_Death)
Code: Select all
local function On_Built(event)
local entity = event.created_entity
--- Add Alien Artifacts to table
if entity.valid and entity.name == "small-alien-artifact" then
global.small_alien_artifact_created[entity.unit_number] = {
artifact = entity,
time = event.tick
}
end
end
On Remove:
Code: Select all
local function On_Remove(event)
local entity = event.entity
--- Remove Alien Artifacts from table
if entity.valid and entity.name == "small-alien-artifact" then
if global.small_alien_artifact_created[entity.unit_number] then
global.small_alien_artifact_created[entity.unit_number] = nil
end
end
end
Code: Select all
local function On_Death(event)
local entity = event.entity
if entity.valid and entity.name == "small-alien-artifact" then
if global.small_alien_artifact_created[entity.unit_number] then
global.small_alien_artifact_created[entity.unit_number] = nil
end
end
end
How should I add and remove these Small Alien Artifact Loot from a table?
Thanks.