Jump to content
  • Advertisement
Sign in to follow this  
Matthew Shockley

Quick Help

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

If you intended to correct an error in the post then please contact us.

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);
	AddObject(Test);
    lua_settable(L, -5);
	return 1;
}

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Matthew Shockley
Why 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 this post


Link to post
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.

See this for more info.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!