diff --git a/maps/includes/base.lua b/maps/includes/base.lua index 59a3e36..5d96d75 100644 --- a/maps/includes/base.lua +++ b/maps/includes/base.lua @@ -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)