add goir() and oir()

This commit is contained in:
whosit 2025-03-25 13:01:52 +03:00
parent ed80ef004d
commit a5a6ba6630
2 changed files with 37 additions and 0 deletions

View File

@ -141,3 +141,14 @@ already existed.
#### `dir()` and `keys()`
List keys of the table (useful for exploring data structures, without flooding your chat).
#### `get_objects_inside_radius()` shortcuts: `goir()` and `oir()`
`goir(radius)` returns a list of objects around you
`oir(radius)` returns an iterator of objects around you
```
> goir(100) -- return a list of objects within 100 units around you
> for v in oir(100) do print((v:get_luaentity() or {}).name) end
```

View File

@ -116,6 +116,32 @@ local function create_shared_environment(player_name)
end
end,
--dump = repl_dump,
goir = function(radius)
local me = core.get_player_by_name(player_name)
if me then
local objs = core.get_objects_inside_radius(me:get_pos(), radius)
return objs
else
return {}
end
end,
oir = function(radius)
local me = core.get_player_by_name(player_name)
if me then
local objs = core.get_objects_inside_radius(me:get_pos(), radius)
local nextkey, v
--local i = 1
return function()
-- FIXME skip invalid objects here?
nextkey, v = next(objs, nextkey)
return v
-- i = i + 1
-- return objs[i]
end
else
return function() return nil end
end
end,
},
{
__index = function(self, key)