lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,

I visited the wiki page "Implementing Virtual Methods With Toluapp"
and followed the instructions there to setup virtual methods in
ToLua++. However, it didn't work. Perhaps I'm doing a mistake. Please,
someone could help me to find it?

For testing purposes, my code is quite similar to the wiki page. The
problem arises when I'm trying to create a new Lua__Widget instance
(line: local w = Lua__Widget:new()). It pops up its constructor (new
function) doesn't exist (it's nil).

In the wiki page it's stated that Lua__Widget has all the constructors
of Widget.

What is happing? Am I missing something?

Thanks in advance,
André.


Here is the code:

// --- widget.lua -----------------------------------------------------

local CustomWidget = {}
CustomWidget.__index = CustomWidget

function CustomWidget:input_event(e)
       print("just for testing")
end

function CustomWidget:new(name)
  local t = {}
  setmetatable(t, CustomWidget)

  local w = Lua__Widget:new()
  tolua.setpeer(w, t)
  w:tolua__set_instance(w)

  return w
end

function create_widget(name)
       return CustomWidget:new(name)
end


// --- widget.cpp ------------------------
[...]
Widget* create_widget(const char *name)
{
       lua_getglobal(L, "create_widget");
       lua_pushstring(L, name);
       if (lua_pcall(L, 1, 1, 0) != 0)
               cerr << "Error: " << lua_tostring(L, -1) << endl;
       return (Widget*)  tolua_tousertype(L, -1, 0);
}

int main()
{
       L = lua_open();
       luaL_openlibs(L);
       luaopen_widget(L);
       if (luaL_loadfile(L, "widget.lua") || lua_pcall(L, 0, LUA_MULTRET, 0)) {
               cerr << "Erro de script:" << endl << lua_tostring(L,-1) << endl;
               exit(1);
       }
       Widget *w = create_widget("aaa");
       return 0;
}

// --- widget.h ---------------------------------------
[...]
class Widget {
       private:
               string name;
       protected:
              virtual bool input_event() {}
       public:
              Widget(string p_name): name(p_name) { }
};


// --- widget.pkg --------------------------------------

$#include "tolua_base.h"
$#include "widget.h"

class Widget {
   protected:
       virtual bool input_event();
   public:
       Widget(string name);
};