Pikachar wrote: ↑Sun Apr 23, 2023 3:47 am
Pi-C wrote: ↑Sat Apr 22, 2023 7:33 am
I added a migration file that would look for all existing vehicles of the prototypes I need, fetched the unique_id of their equipment grids, and stored them as
Code: Select all
global.grids[LuaEquipmentGrid.unique_id] = LuaEntity.unit_number
Right, but how exactly did you get the list of all existing vehicles? The only way I can think of to do it is incredibly slow.
If you add a migration file, it will be run only once in each game. Looking for all entities may take a while but that's OK if it's only done once. This is how I would do it:
Code: Select all
local my_types = {"car", "spider-vehicle"}
local grid
global.grids = global.grids or {}
for s, surface in pairs(game.surfaces) do
for v, vehicle in pairs(surface.find_entities_filtered{type = my_types}) do
grid = vehicle.grid
if grid and grid.valid then
global.grids[grid.unique_id] = vehicle.unit_number
end
end
end
And just to make sure I'm understanding correctly, you're keeping track of them in a table of {gridID, entityUniqueIdentifier}.
I thought I did, but I've actually used
Code: Select all
global.grids[grid.unique_id] = vehicle
This has the advantage that grids from vehicles that have been destroyed or mined will automatically be removed from global.grids. You actually need an entity, the unit_number is only useful if you can look up the entity from it. But I've another table for storing all vehicles currently handled by my mod:
Code: Select all
global.vehicles[vehicle.unit_number = {
entity = vehicle,
id = vehicle.unit_number,
grid_id = vehicle.grid and vehicle.grid.valid and vehicle.grid.unique_id,
…,
}
So, if defines.events.on_equipment_inserted was triggered, this is how I would get the entity associated with that grid via unit_number:
Code: Select all
script.on_event(defines.events.on_equipment_inserted, function(event)
local grid_id = event.grid.unique_id
local unit_number = global.grids[grid_id]
local vehicle = unit_number and global.vehicles[unit_number] and global.vehicles[unit_number].entity
if vehicle and vehicle.valid then
--
end
end)
This would be useful because most of my functions deal with the data I've stored with the vehicle, not the vehicle itself, so storing grid.unique_id together with vehicle.unit_number would allow me faster access to the data stored in global.vehicles[vehicle.unit_number]. The disadvantage is that I must manually remove entries from global.grids when an entity is destroyed, mined, etc.
When do these particular events occur exactly?
on_built_entity - is this on entity placement? is it just by the player or also by say robots?
This is called when a player or a bot place an entity.
on_entity_cloned - When exactly does cloning occur? Is this when a copy/pasta is made, or a blueprint is made, or both? Or is it just blueprint placement?
This is raised by
LuaSurface.clone_entities.
script_raised_built, and script_raised_revive - not sure that I would personally need these for what I'm trying to so since I'm not telling anything to build/revive. But good to know about.
These events are for interaction of mods. For example, my mod could silently create an entity:
Code: Select all
local pos = {player.position.x + 10, player.position.y}
player.surface.create_entity{name = "car", position = pos, force = player.force}
Your mod would never know about this, so you couldn't update your grid table. But I could raise an event when I create my car, so all other mods who listen to script_raised_built can do whatever they need with it:
Code: Select all
local pos = {player.position.x + 10, player.position.y}
player.surface.create_entity{name = "car", position = pos, force = player.force, raise_built = true}