-1

I have a problem with luabind, or at least i expect its a problem

I have an entity class that ive registered with lua,

ideally i want to subclass it and override its functions, from there i want to send it back to c++ and store it

additionally i want to be able to call its new functions from c++ from the stored object/pointer

however im currently struggling to even get c++ to take the object of type cEntity* back? in the lua script i can load the class, call its variables and functions, i try and send it to takeClass or takebOject but it comes out as a blank class with nothing set on it

for example foo->name is "" rather than "Entity1" and id is 0 rather than 1

anyideas what im doing wrong? ive searched on google for at least a week now with no luck of understanding this problem and its completely halting my progress on my project?

//#######################################################################
// Test function
//#######################################################################
void luaTest::TakeClass(cEntity* foo)
{

    cout << foo->name << endl;
}

void luaTest::TakeObject(luabind::object foo)
{
    cEntity* foobar = luabind::object_cast<cEntity*>(foo);
    cout << foobar->name << endl;
}

void luaTest::luabindClass(lua_State* L)
{
    //Somewhere else
    module(L)
        [
            class_<luaTest>("luaTest")
            .def(constructor<>())
            .def("TakeClass", &luaTest::TakeClass)
            .def("TakeObject", &luaTest::TakeObject)
        ];
    globals(L)["test"] = this;
}


//#######################################################################
// Entiy Class
//#######################################################################

class cEntity
{
public:
    string name;
    int id;

    cEntity();
    ~cEntity();

    static void luabindClass(lua_State* L);
};

//#######################################################################
cEntity::cEntity()
{
    name = "NotSet";
    id = 0;
}


cEntity::~cEntity()
{
}

void cEntity::luabindClass(lua_State* L)
{
    module(L)
        [
            class_<cEntity>("cEntity") 
            .def(constructor<>()) 
            .def_readwrite("name", &cEntity::name)
            .def_readwrite("id", &cEntity::id)
        ];
}


//#######################################################################
// Lua File
//#######################################################################
entity = cEntity();
entity.name = "Entity1";
entity.id = 1;

test:TakeClass(entity);
test:TakeObject(entity);
//#######################################################################

//#######################################################################
// main

//#######################################################################
....
/* run the script */
if (luaL_dofile(L, "avg.lua")) {
    std::cout << lua_tostring(L, -1) << std::endl; // Print out the error message
}
....
//#######################################################################
Saragan
  • 75
  • 3
  • 10

1 Answers1

0

https://gist.github.com/take-cheeze/7264dbf1ea6e08a2d24a

#include <iostream>
#include <string>
#include "luabind/luabind.hpp"

extern "C" {
#include "lauxlib.h"
#include "lualib.h"
}



class cEntity
{
 public:
  std::string name;
  int id;

  cEntity();
  ~cEntity();


  std::string getName() { return name; }
  void setName(std::string n) { name = n; }

  int getID() { return id; }
  void setID(int n) { id = n; }


  virtual void testFunction(){};
  static void luabindClass(lua_State* L);
};

//#######################################################################
// Test function
//#######################################################################
struct luaTest {
  void TakeClass(cEntity* foo)
  {
    std::cout << foo->name << std::endl;
  }

  void TakeObject(luabind::object foo)
  {
    cEntity* foobar = luabind::object_cast<cEntity*>(foo);
    std::cout << foobar->name << std::endl;
  }

  void luabindClass(lua_State* L)
  {
    //Somewhere else
    luabind::module(L)
        [
            luabind::class_<luaTest>("luaTest")    // < "Animation" how we want to name the Class in the Lua runtime
            .def(luabind::constructor<>())
            .def("TakeClass", &luaTest::TakeClass)
            .def("TakeObject", &luaTest::TakeObject)
         ];
    luabind::globals(L)["test"] = this;
  }
};


//#######################################################################
// Entiy Class
//#######################################################################

//#######################################################################
cEntity::cEntity()
{
  name = "NotSet";
  id = 0;
}


cEntity::~cEntity()
{
}

void cEntity::luabindClass(lua_State* L)
{
  luabind::module(L)
      [
          luabind::class_<cEntity>("cEntity")    // < "Animation" how we want to name the Class in the Lua runtime
          .def(luabind::constructor<>())            // < Binds the empty constructor
          .def_readwrite("name", &cEntity::name)
          .def_readwrite("id", &cEntity::id)
       ];
}


char const* script =
                 "entity = cEntity();\n"
                 "entity.name = \"Entity1\";\n"
                 "entity.id = 1;\n"
                 "\n"
                 "test:TakeClass(entity);\n"
                 "test:TakeObject(entity);\n";

int main() {
  lua_State* L = lua_open();
  luabind::open(L);

  cEntity::luabindClass(L);
  luaTest test;
  test.luabindClass(L);
  /* run the script */
  if (luaL_dostring(L, script)) {
    std::cout << lua_tostring(L, -1) << std::endl; // Print out the error message
  }
  lua_close(L);
}

Printed:

$ ccache clang++ -lluabind -llua -g3 ~/test.cxx && ./a.out
Entity1
Entity1

So there maybe problem in script loading. Is there any error message printed?

(Sorry that I can't answer this in IRC. Took a while to create testing env)

otus
  • 5,572
  • 1
  • 34
  • 48
  • When answering with code, please post it in code balise here. Why? Because if in X years github is down, the one reading this can still have the answer ; ) – DrakaSAN Jun 26 '14 at 08:25
  • Thanks for the fast reply, apologies for not staying in irc as I had to go to work (still am heh) um there were no errors in the printed out, I added a check in the takeClass and takeObject as well as the do_file just to be sure is there a particular version of lua/luabind I should be using? cause I ran your code and I got the same thing as before? I know im using lua5.1, I grabbed luabind from the main site? – Saragan Jun 26 '14 at 08:28
  • @Saragan I'm using luabind that can be installed with `brew install luabind`(seems like it's 0.9.1). And lua's version is 5.1.5. – take-cheeze Jun 26 '14 at 08:36
  • Ok, will un-install lua and luabind and try again with those versions, it may just be a mismatch – Saragan Jun 26 '14 at 08:37
  • ok, well quickly downloaded the lua 5.1.5 prebuild binaries from http://sourceforge.net/projects/luabinaries/files/5.1.5/Windows%20Libraries/Dynamic/ installed them into vc and sys32/64 built the latest luabind library 0.9.1 which threw up no errors in build (making sure I set correct boost path and lua_path(just the binary download file)) and im still getting the same issue? currently using visual studio 2012? ive included lua5.1.lib and luabindd.lib and included the boost directory as additional – Saragan Jun 26 '14 at 08:58
  • I managed to get it working. I downloaded the forked version of the library https://github.com/rpavlik/luabind from there I used cMake gui on windows to build (being careful to set the lua library/include path) after that I opened the build project in VC and did a build all, then build test (3 failed rather than the 49 that it did the times before), then build install :) it installed it on my c drive, I moved them to a more desirable location and now take-cheese's example works :) thanks guys for your input :) – Saragan Jun 26 '14 at 13:02