find_entity(entity, position)
find_entities(area)
find_entities_filtered{area=…, name=…, type=…, force=…, limit=…}
find_enemy_units(center, radius, force
find_nearest_enemy{position=…, max_distance=…, force=…}
all in LuaSurface by the way.
now, these are alright if you want to find out what is near you, but what if you're doing some sort of migration, scanning to see what exists so you can hook a script to it...
from what I can tell, you have to scan an area, and see if the entity type is there....
The game obviously knows all entities that exist, since it updates them with the built in functionality, so... why can't we search through a list of all entities that exist everywhere?
game.entities, being a table of all entities everywhere.
LuaSurface.entities being a list of all entities on that surface.
Somehow, I think using a hook like that would be a lot faster than...
Code: Select all
local entities = game.surfaces[1].find_entities_filtered{area = {{-1000000, -1000000}, {1000000, 1000000}}, type= "inserter"} -- find and save a table of every inserter on the whole possible map
Code: Select all
for i, entity in pairs(game.surfaces[1].entities) do -- iterate through entities
if entity.type = "inserter" then -- it's an inserter, do something to it
Code: Select all
local entities = game.surfaces[1].find_entities_filtered{type= "inserter"} -- find and save a table of every inserter on the surface
since searching a grid of 2 million by 2 million takes... effectively forever, I had to narrow the area down to 100 thousand by 100 thousand, which takes about 10 seconds on my rather fast computer. something at least remotely sane. but if someone built an inserter further than this away from the middle of the map, it would be missed.
Do you see where I'm coming from? Is there any technical limitation to prevent this from being possible? (or faster than an area scan)
I mean, you scan the maximum possible map size, it's obviously checking every single tile position even if nothing is there because of how long it takes. so, on larger areas, iterating through a table of entities and checking if it is within the specified area would be far faster in my opinion, as well as allowing for checking the entire surface, regardless of position.