lua-users home
lua-l archive

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


Hello all,

I'm new to Lua, but I think that it is an excellent scripting language and would like to use it in combination with C++. I've read a good deal of http://www.lua.org/pil/ , especially "Part IV. The C API", but apparently due to my lack of experience with Lua, I've not been able to my problem myself.

The problem is that I have a simple hierarchy (a tree) of C++ objects, as for example windows that can be found in a GUI system:
(For clarity, I'm just providing pseudo-code, it will probably not compile.)

class Window
{
    public:

    Window* Find(char* n);    // Find a window with name "n" in the

hierarchy tree of this window.

void Draw(); // Draws this window on the screen - implemented in C++. void SomeMethod(float f); // Some helper method for demonstration purposes, also implem. in C++. void OnMouseClick(); // Some event handler - supposed to be implemented by a Lua script!

std::string name; // Name of this window, e.g. for reference in Lua scripts. int pos[2]; // The position of the top-left corner of this window.
    int         size[2];      // The size of this window in pixels.
    float       back_col[3];  // The background color.
    float       text_col[3];  // The text / foreground color.
    std::string text;         // Text to be displayed in the window.

    Window*         parent;   // The parent of this window (may be NULL).
    vector<Window*> children; // The sub-windows of this window (e.g.

the buttons in a dialog).
};

Again, you can use Lunar for the OnClick call-back
here is what I do :
---------------------------------------------------------------------------
void LuaMenu::OnDispatch(void *ptr1,void *ptr2,const string & cmd)
{
     Gui_dialog *d = (Gui_dialog *)ptr1;
     Gui_gadget *g = (Gui_gadget *)ptr2;
     lua_State *L = (lua_State *)d->user_info();
       //find a way to push dialog and gadget on lua stack
     /* push functions and arguments */
     lua_getglobal(L, cmd.c_str());
     LuaDialog dialog(L,d);
     Lunar<LuaDialog>::push(L,&dialog);
     LuaGadget gadget(L,g);
     Lunar<LuaGadget>::push(L,&gadget);


     /* do the call (0 arguments, 0 result) */
     if (lua_pcall(L, 2, 0, 0) !=0)
     {    char buf[500];
       sprintf(buf, "error running function `f': %s",
                lua_tostring(L, -1));
       MessageBox(NULL,wstr(buf),L"Error",MB_OK);
     }
}
-------------------------------------------------------

In the lua world you end up with a dialog and gadget "object" that you can use "dialog:method"

Maybe it will help
Best wishes

--
Noël Frankinet
Gistek Software SA
http://www.gistek.net