Allow GlobalEntityList to be iterated by pairs and ipairs

* See 6309529590

Example usage:

for ent_id, ent in pairs(GlobalEntityList) do
  print(ent_id, ent)
end

Note: The order of iteration is always arbitrary
This commit is contained in:
squeek 2015-03-23 22:13:03 -07:00
parent 171376e24c
commit e8c59ff5ec

View file

@ -49,6 +49,47 @@ function baseclass:new (o)
end
-----------------------------------------------------------------------------
-- set up pairs and ipairs for iterating the global entity list
--
-- Example usage:
--
-- for ent_id, ent in pairs(GlobalEntityList) do
-- print(ent_id, ent)
-- end
--
-- Note: The order of iteration is always arbitrary
-----------------------------------------------------------------------------
local GlobalEntityListIterator = function()
local entity = GlobalEntityList:FirstEntity()
return function()
local cur_ent = entity
entity = GlobalEntityList:NextEntity(cur_ent)
if cur_ent then
return cur_ent:GetId(), cur_ent
else
return nil
end
end
end
local ipairs_base = ipairs
ipairs = function(t)
if t == GlobalEntityList then
return GlobalEntityListIterator()
end
return ipairs_base(t)
end
local pairs_base = pairs
pairs = function(t)
if t == GlobalEntityList then
return GlobalEntityListIterator()
end
return pairs_base(t)
end
-----------------------------------------------------------------------------
-- make luabind's class_info function safer
-- (don't crash if class_info() is called on non-luabind objects)