lua-users home
lua-l archive

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


Carsten Fuchs wrote:

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).
};


As mentioned in the comments, I'd like to use Lua to implement the OnMouseClick() method of each Window, so that each Window has an individual implementation if desired.

Here is an example for an implementation of the OnMouseClick() method as I'd write it in C++:

void Window::OnMouseClick()
{
    // The contents of this method is supposed to
    // be implemented with a Lua script...

    back_col[0]=0.0f;
    back_col[1]=0.3f;
    back_col[2]=0.9f;

    SomeMethod(0.2f);

    size[0]=100.0f;
    size[1]=20.0f;

    text="I was clicked!";


    WindowT* other=Find("Button OK");

    other->SomeMethod(3.0f);
    other->pos[0]=80.0f;
    other->text="OK";

    parent->text="My parent too...";

    // and so on...
}


So in spite of having read the PIL book, I have not the faintest idea what the best approach to this problem is: I might be able to model a table hierachy in Lua that mirrors the Window hierachy in C++, but what then? Don't I have to manually "synchronize" both hierarchies after each call from/to Lua/C++?? Should I use userdata instead, analogous to http://www.lua.org/pil/28.1.html?

Are there any C++ specific examples where I can learn how this problem should be approached?

Hello,
I am still very new to lua, but I've found that the Lunar template is very easy to use and keep a "object orientation" on the lua side .
Did you try it ?


Any help would much be appreciated!
Thank you very much for your time!

Best regards,
Carsten





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