lua-users home
lua-l archive

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


> But I think this is the magic answer for my question: I need to put the value in the metatable itself.

That's how I would do it. As I'm fairly new to Lua, I don't know if that's the canonical way of doing things or if it's considered atrocious... but it's simple and it should work ;)

> I hope this was my last email about this :-)

I hope not - share your success (or failure) story, I'd be interested how it turned out :)


On Tue, Sep 16, 2014 at 12:31 PM, Dirk Schippers <lua@frixx-it.com> wrote:
Hi Udo,


Udo Schroeter schreef op 16/09/2014 12:12:
Again, it's helpful to look at the Lua source itself, for example the liolib.c which I mentioned earlier.

Yes yes, I have done this a lot before and solved other problems that way, but I can't find this one.
The steps involved would be:

1) create the userdata object
2) make a new table and set it as the metatable for the object

^ this gives you a C struct linked with a Lua table, you don't have to do any housekeeping to keep them associated.

3) Now, from the user side, you probably want to make changing values in that table easier for them. That's where you could pre-populate the metatable with a __newindex and an __index function that takes care of storing and retrieving values - so people can use the idiom you described with the onMouseMove example.

Yes, I have done these things before a lot, I have created lots of objects before.
For this, you'd have to make a little C function that takes the incoming values and saves them to the metatable itself.
But I think this is the magic answer for my question: I need to put the value in the metatable itself.
I think that for me, the metatable has always been some kind of magical object but it is actually just a table associated to another object that I can put values in. I think I understand now and I will try it!
Thanks.


4) Using the table from the C side is simple. You retrieve the metatable for your struct, look up the pertinent function and call it if present. You can find examples for this throughout the Lua *lib.c files as well.
And then, yes, of course, it is easy. I just push the user data, get the meta table, call its onMouseMove function (if it exists).


I hope this helps you get on your way. It takes a little effort to get acquainted with the Lua API, but once you get the hang of it, everything should fall into place...

Yes, thanks! I have done a lot of projects in Lua before and I have never had any problems with it because I always sticked to Lua objects or C objects and not really interact between them as this was usually not necessary.
I hope this was my last email about this :-)