Access Light user data in Lua

Perhaps I misunderstand their use or read the documentation incorrectly, but how do I access members of a structure or class passed to Lua as light user data? For example, if a vector using the following structure

typedef struct Foo { int x; int y; } Foo; 

was declared as a β€œtest” and defined as something like x = 413 and y = 612 and pressed with a call

 lua_pushlightuserdata( L, (void *) &test ) ; 

What would the Lua code of a function look like that manipulates or prints x and y?

+2
c ++ lua lua-userdata
source share
2 answers

Light user data in Lua is just a pointer. Lua does not know how to read the contents of the object referenced by the pointer. The purpose of this data is to allow one C API call to pass a link to another API call.

If you want to work with data in Lua, you need to push it onto the stack, and in your case I would make it a table. If you need this data synchronized between C and Lua, you will need to write functions on both sides to handle the assignment and copy the value to another environment. Another option is to only have data in Lua and retrieve it through the Lua API in C, or have it in C and retrieve it through the C API (which you will create) when in Lua.

For more information , Lua Programming and the Lua Wiki Users, each has a short description.

+11
source share

Nothing. You cannot access or do anything with userdata of any kind from Lua. You must provide C ++ functions for all manipulations. Of course, this can be simplified by the proper use of metaphors.

Oh, and you don't need to typedef struct shit in C ++. You can just say

 struct Foo { int x; int y; }; 
+2
source share

All Articles