title: How do I pass a list of objects from C++ to Lua?
body: <p>I'm the lead dev for <a href="http://bitfighter.org/" rel="noreferrer">Bitfighter</a>, and am adding user-scripted bots using Lua.  I'm working with C++ and Lua using Lunar to glue them together.</p>
  
  <p>I'm trying to do something that I think should be pretty simple: I have an C++ object in Lua (bot in the code below), and I call a method on it that (findItems) which causes C++ to search the area around the robot and return a list of objects it finds (TestItems and others not shown here).  My question is simply how do I assemble and return the list of found items in C++, and then iterate over them in Lua?</p>
  
  <p>Basically, I want to fill in the <i>&lt;&lt;&lt;&lt; Create list of items, return it to lua >>>></i> block below, and make any corrections I may need in the Lua code itself, included below that.  </p>
  
  <p>I've tried to keep the code simple but complete.  Hope there's not too much here!  Thanks!</p>
  
  <p><b>C++ Header file</b></p>
  
  <pre><code>class TestItem : public LuaObject
  {
  
  public:
     TestItem();     // C++ constructor
  
     ///// Lua Interface
  
     TestItem(lua_State *L) { } ;             //  Lua constructor
  
     static const char className[];
     static Lunar&lt;TestItem&gt;::RegType methods[];
  
     S32 getClassID(lua_State *L) { return returnInt(L, TestItemType); }
  };
  
  
  class LuaRobot : public Robot
  {
     LuaRobot();     // C++ constructor
  
     ///// Lua Interface
  
     LuaRobot(lua_State *L) { } ;             //  Lua constructor
  
     static const char className[];
     static Lunar&lt;LuaRobot&gt;::RegType methods[];
  
     S32 findItems(lua_State *L);
  }
  </code></pre>
  
  <p><b>C++ .cpp file</b></p>
  
  <pre><code>const char LuaRobot::className[] = "Robot";      // Class name in Lua
  // Define the methods we will expose to Lua
  Lunar&lt;LuaRobot&gt;::RegType LuaRobot::methods[] =
  {
     method(LuaRobot, findItems),
     {0,0}    // End method list
  };
  
  
  S32 LuaRobot::findItems(lua_State *L)
  {
     range = getIntFromStack(L, 1);    // Pop range from the stack
     thisRobot-&gt;findObjects(fillVector, range);  // Put items in fillVector
  
     &lt;&lt;&lt;&lt; Create list of items, return it to lua &gt;&gt;&gt;&gt;
  
     for(int i=0; i &lt; fillVector.size(); i++)
        do something(fillVector[i]);    // Do... what, exactly?
  
     return something;
  }
  
  
  /////
  const char TestItem::className[] = "TestItem";      // Class name in Lua
  
  // Define the methods we will expose to Lua
  Lunar&lt;TestItem&gt;::RegType TestItem::methods[] =
  {
     // Standard gameItem methods
     method(TestItem, getClassID),
     {0,0}    // End method list
  };
  </code></pre>
  
  <p><b>Lua Code</b></p>
  
  <pre><code>bot = LuaRobot( Robot ) -- This is a reference to our bot
  
  range = 10
  items = bot:findItems( range )
  
  for i, v in ipairs( items ) do
      print( "Item Type: " .. v:getClassID() )
  end
  </code></pre>