"count" parameter for LuaSurface.get_closest
Posted: Thu Jun 24, 2021 10:18 pm
LuaSurface.get_closest() is very useful. I'm pretty sure it's orders of magnitude faster than sorting the list of entities on the Lua side, given it would need to constantly query the engine for entity positions and such.
I would very much like to extend this function with an optional "count" parameter. This would allow me to get, say, the 5 closest entities from the given list. Currently I have to do this by getting the closest, finding and removing it from the list, getting the next closest... Or sorting the list myself on the Lua side and taking the first N.
This would, however, be a breaking change as it would change the return type from LuaEntity to LuaEntity[], but this could be mitigated: if "count" parameter is not passed, it returns the single LuaEntity, but if the "count" parameter is passed (even if it is 1!) then an array is returned instead. Alternatively, add LuaSurface.get_n_closest() as a separate function.
I would very much like to extend this function with an optional "count" parameter. This would allow me to get, say, the 5 closest entities from the given list. Currently I have to do this by getting the closest, finding and removing it from the list, getting the next closest... Or sorting the list myself on the Lua side and taking the first N.
This would, however, be a breaking change as it would change the return type from LuaEntity to LuaEntity[], but this could be mitigated: if "count" parameter is not passed, it returns the single LuaEntity, but if the "count" parameter is passed (even if it is 1!) then an array is returned instead. Alternatively, add LuaSurface.get_n_closest() as a separate function.