From e8c59ff5ec02e0267b827be69f199b0a08a77016 Mon Sep 17 00:00:00 2001 From: squeek Date: Mon, 23 Mar 2015 22:13:03 -0700 Subject: [PATCH] Allow GlobalEntityList to be iterated by pairs and ipairs * See https://github.com/fortressforever/fortressforever/commit/63095295902222c10d703410a20dded66beca561 Example usage: for ent_id, ent in pairs(GlobalEntityList) do print(ent_id, ent) end Note: The order of iteration is always arbitrary --- maps/includes/base.lua | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) 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)