lua-users home
lua-l archive

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


Hi,

I am creating a test project (C++) where I have this telnet server which creates a thread for every connected client that has its own Lua environment to play around in.
But I want to do more than: print 5 + 2
So I created some functions: createWindow({...}) which creates a window. I can call it multiple times to create multiple windows.

At this time, the function has become the constructor for an object, which returns a light userdata pointing to a C-struct containing the window handle and some other stuff.

But I want to expand this: I want the return value to become a table where you can put your own events handlers in, like this:

w = window.new({...})
w.onMouseMove = function(x, y)
    ...
end

I would also like to be able to pass the event function immediately to the constructor as well but still keep the ability to update the event later on.

Now the question is, how do I handle this: do I create a table (instead of a light userdata) in the constructor for window and add a metatable to it? How should I handle the optional event declaration? Do I need to override the __index or should I keep all of it in lua? How can I call the correct event function in the correct object when I receive the events in my message loop? If I use tables, I don't know how to find them back... How can I keep an index between the window handle and a lua table? Where do I put the window-handle and stuff like that, whilst keeping the table safe so that noone can change this window handle through Lua code and corrupt the environment? Do I put an extra light userdata inside the table? Can I protect that one?

I know this is a big question but I have the feeling that the answer will be simpler.

Dirk.