Quick Help

This topic is 3187 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Why does this return a function value when its called. It should return the table.
static int Object(lua_State* L) {
const char *val = lua_tostring(L, -1);
lua_newtable(L);
lua_pushvalue(L, -1);
lua_setmetatable(L, -2);
lua_pushliteral(L, "__index");
lua_pushcfunction(L, __index);
lua_pushliteral(L, "__tostring");
lua_pushcfunction(L, __tostring);
Triangle Test;
Test.Pointer = (int*)lua_topointer(L, -5);
lua_settable(L, -5);
return 1;
}


Anyone?

Share on other sites
Quote:
 Original post by Matthew ShockleyWhy does this return a function value when its called. It should return the table.*** Source Snippet Removed ***

It looks like you are only calling settable once at the end, you should call it twice (as it looks like you are putting two things in your table. This returns a function because the thing on top is a function (because you didn't take it off and put in the table). The function you are returning is __index (__tostring got settabled).

Could you comment it a bit so we can see what your doing? Your style is much diff from mine, and Lua API is not very self-documenting.

Lets see if I understand correctly:

Takes one string argument (I would use positive indicies for args, in case they pass one extra or something), which is unused right now.

You create a table, make it it's own metatable, and then put an __index and __tostring cfunction in it (you should use settable immediately after pushing a name/val pair, so you have minimum stuff to think about on the stack).

Then you are doing something wierd with a traingle (comments please?). Keep in mind that topointer is NOT reversible, and not garbage collected.

The settable should go after the pushes, as I mentioned above.

Do I comprehend?

Share on other sites
Oh, I only thought you used settable once. Thanks.

Share on other sites
I meant to mention this earlier, C identifiers that begin with double underscores are reserved for the compiler implementation and should not be used. In other words, don't name your C functions __index or __tostring.

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 30
• 16
• 11
• 10
• 11
• Forum Statistics

• Total Topics
634113
• Total Posts
3015591
×