Unique ID

Started by
9 comments, last by ddn3 13 years, 11 months ago
In my program, users make an unknown amount of userdata values with my function. I make a new Triangle object for each userdata they create and somehow I need to link each Triangle to each userdata for later use. I was thinking about using lua_topointer() but some people think I should use the lua registry. What would be the best way to do this with an unknown amount of userdatas created.
If I asked you for a hundred dollars would the answer to that question be the same as the answer to this question?
Advertisement
If the userdata is 1:1 for the Lua object the user data itself can be the key for the data in the registry. However there might be issues with down casting from a ptr to a signed int. The registry within Lua itself is just a globally accessible table.

I believe its possible to push the user data as an up value onto a c-functor, thus allowing you to give each instance of the Lua Triangle in your case a unique functor. I see your setting the __index explicitly in C perhaps you can bind the userdata/Triangle there? Look into up values they might help.

see..

http://www.lua.org/pil/27.3.3.html

Good Luck!

-ddn
I read the article, but I didn't understand it entirely. Do you know any sample code for it. Meanwhile, I will be looking up how to use lua_upvalueindex(). Thanks!
If I asked you for a hundred dollars would the answer to that question be the same as the answer to this question?
How would I do this with a userdata. It only works with functions from what I can see.
If I asked you for a hundred dollars would the answer to that question be the same as the answer to this question?
Here is my code so far:

static int index(lua_State* L) {	lua_pushstring(L, "Please work!");	return 1;}static int Object(lua_State* L) {	const char *val = lua_tostring(L, -1);	lua_newuserdata(L, 0);	int uTop = lua_gettop(L);	lua_newtable(L);	lua_setmetatable(L, -2);	lua_getmetatable(L, -1);	int Top = lua_gettop(L);	lua_pushliteral(L, "__index");	lua_pushcfunction(L, index);	lua_settable(L, Top);	lua_pushvalue(L, uTop);	return 1;}
If I asked you for a hundred dollars would the answer to that question be the same as the answer to this question?
I think it will be clear if you look at the implementation of

#define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0)

when you use lua_pushcfunction to push the function (static int index(lua_State* L) onto the Lua stack, your pushing a closure onto the stack. Upvalues are used to associate state specific data with C closures. So on the lines..

..
int Top = lua_gettop(L);
lua_pushliteral(L, "__index"); <------ push upvalue around here somewhere
lua_pushcfunction(L, index); <------ push upvalue around here somewhere
lua_settable(L, Top);

the upvalue would just be the user data associated with this triangle object you just created.

After that is done when the function (static int index) is called it can pull the user data from the upvalue associated with the c-closure, and that tells you which real C object Triangle to operate upon. This is a common technique used by most binding libraries.

Good Luck!

-ddn
I think I see what you mean? Make a closure for the index function and then use it to find the userdata? But when I get the userdata value in the index function, how will I check to see if has the same "ID" of a userdata?
If I asked you for a hundred dollars would the answer to that question be the same as the answer to this question?
What are u putting in the user data? Is it the C side triangle object? Which you want to recover to operate upon when __index is called? Thats the normal use case of userdata (application specific ptr data).

I assumed your creating a new triangle object every time (static int Object(lua_State* L)) is called. This creates a Lua table with the associated metatable __index function which pipes to your own index function. Once this userdata to object connection is made it can't be broken, unless you reassign the __index metatable function directly in Lua between objects.

If that's the case, inside your index function, you can recover the userdata then cast it to your real triangle type and operate upon it as so. That's if i'm reading what you want to do right.

Good Luck!

-ddn
I have a Triangle class and every time the Object function is called it makes a userdata and a Triangle. I need to link them like:

Triangle.Userdata = luauserdata;

Or something like that in the index function I can do this:

if (luauserdata == Triangle.Userdata) {
// Do stuff
}
If I asked you for a hundred dollars would the answer to that question be the same as the answer to this question?
Anyone? I am completely stuck on this.
If I asked you for a hundred dollars would the answer to that question be the same as the answer to this question?

This topic is closed to new replies.

Advertisement