diff --git a/control.lua b/control.lua index 5c6d772..3c8a422 100644 --- a/control.lua +++ b/control.lua @@ -37,7 +37,9 @@ script.on_load(on_load) script.on_configuration_changed(on_configuration_changed) script.on_event(defines.events.on_built_entity, on_built_entity) +script.on_event(defines.events.script_raised_built, script_raised_built) script.on_event(defines.events.on_entity_died, on_entity_died) +script.on_event(defines.events.script_raised_destroy, script_raised_destroy) script.on_event(defines.events.on_player_placed_equipment, on_player_placed_equipment) script.on_event(defines.events.on_player_removed_equipment, on_player_removed_equipment) script.on_event(defines.events.on_pre_player_mined_item, on_pre_player_mined_item) diff --git a/src/main.lua b/src/main.lua index e42255d..3d6aaf7 100644 --- a/src/main.lua +++ b/src/main.lua @@ -34,6 +34,7 @@ local fuel_tick local handle_train_state_change local on_entity_removed local on_train_removed +local on_entity_added local read_equipment local recover_energy local update_equipment @@ -333,21 +334,35 @@ on_entity_removed = function(entity) end end +on_entity_added = function(entity) + if(entity and entity.valid) then + if(vehicle_types[entity.type] and entity.grid) then + global.vehicles[entity.unit_number] = entity + update_equipment(entity.unit_number, entity.grid) + end + end +end + -- Event entry points ------------------------------------------------------------------------------- function on_built_entity(event) - local entity = event.created_entity - if(vehicle_types[entity.type] and entity.grid) then - global.vehicles[entity.unit_number] = entity - update_equipment(entity.unit_number, entity.grid) - end + on_entity_added(event.created_entity) +end + +function script_raised_built(event) + on_entity_added(event.entity) -- script_raised has different structure than on_built_entity end function on_entity_died(event) on_entity_removed(event.entity) end +function script_raised_destroy(event) + on_entity_removed(event.entity) +end + + function on_player_placed_equipment(event) for unit, entity in pairs(global.vehicles) do if(entity.valid) then