Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 218803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:47:08+00:00 2026-05-11T18:47:08+00:00

I’m the lead dev for Bitfighter , and am adding user-scripted bots using Lua.

  • 0

I’m the lead dev for Bitfighter, and am adding user-scripted bots using Lua. I’m working with C++ and Lua using Lunar to glue them together.

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?

Basically, I want to fill in the <<<< Create list of items, return it to lua >>>> block below, and make any corrections I may need in the Lua code itself, included below that.

I’ve tried to keep the code simple but complete. Hope there’s not too much here! Thanks!

C++ Header file

class TestItem : public LuaObject
{

public:
   TestItem();     // C++ constructor

   ///// Lua Interface

   TestItem(lua_State *L) { } ;             //  Lua constructor

   static const char className[];
   static Lunar<TestItem>::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<LuaRobot>::RegType methods[];

   S32 findItems(lua_State *L);
}

C++ .cpp file

const char LuaRobot::className[] = "Robot";      // Class name in Lua
// Define the methods we will expose to Lua
Lunar<LuaRobot>::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->findObjects(fillVector, range);  // Put items in fillVector

   <<<< Create list of items, return it to lua >>>>

   for(int i=0; i < 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<TestItem>::RegType TestItem::methods[] =
{
   // Standard gameItem methods
   method(TestItem, getClassID),
   {0,0}    // End method list
};

Lua 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:47:08+00:00Added an answer on May 11, 2026 at 6:47 pm

    So you need to fill a vector and push that to Lua.
    Some example code follows. Applications is a std::list.

    typedef std::list<std::string> Applications;
    

    I create a table and fill it with the data in my list.

    int ReturnArray(lua_State* L) {
        lua_createtable(L, applications.size(), 0);
        int newTable = lua_gettop(L);
        int index = 1;
        Applications::const_iterator iter = applications.begin();
        while(iter != applications.end()) {
            lua_pushstring(L, (*iter).c_str());
            lua_rawseti(L, newTable, index);
            ++iter;
            ++index;
        }
        return 1;
    }
    

    This leaves me with an array in the stack. If it were returned to Lua, then I could write the following:

    for k,v in ipairs( ReturnArray() ) do
        print(v)
    end
    

    Of course so far, this just gets me a Lua array of strings. To get an array of Lua objects we just tweak your method a bit:

    S32 LuaRobot::findItems(lua_State *L)
    {
        range = getIntFromStack(L, 1);    // Pop range from the stack
        thisRobot->findObjects(fillVector, range);  // Put items in fillVector
    
        // <<<< Create list of items, return it to lua >>>>
    
        lua_createtable(L, fillVector.size(), 0);
        int newTable = lua_gettop(L);
        for(int i=0; i < fillVector.size(); i++) {
            TestItem* item = fillVector[i];
            item->push(L);  // put an object, not a string, in Lua array
            lua_rawseti(L, newTable, i + 1);
        }
        return 1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If they are vectors, you could use an SVG Editor… May 11, 2026 at 11:42 pm
  • Editorial Team
    Editorial Team added an answer There is no need to set the select, only the… May 11, 2026 at 11:42 pm
  • Editorial Team
    Editorial Team added an answer I don't think that an increase of about 1 Mb… May 11, 2026 at 11:42 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.