Jump to content
  • Advertisement
Sign in to follow this  
swordfish

Using Lua Userdata

This topic is 4669 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

I've got the following Lua5 script which is the frontend to a very basic interface to my engine's SceneManager and scene graph:
--Lua5
GSceneManager = SceneManager()
PKNode = SceneNode("root")
print(PKNode)
GSceneManager:Attach(PKNode)


Note: I'm using "Lunar" which is basically a toolkit to speed up and ease the C++->Lua binding process - it's not exactly automatic, but it sure does help noobs like me. If you haven't heard of it, it basically makes the whole binding process (metatables, registry, etc.) less tedious through a C++ template. Line 1 in my script creates a userdata class instance LuaSceneManager, which "wraps" the SceneManager singleton in my engine. Line 2 creates a LuaSceneNode userdata class instance PKNode, which instantiates/stores/wraps a Node instance. Line 3 prints the memory address of the LuaSceneNode userdata class instance PKNode. Line 4 passes the LuaSceneNode userdata PKNode to the LuaSceneManager member function "Attach", which is implemented in C++. This is where I'm running into troubles:
int LuaSceneManager::Attach(lua_State *pkLuaVM)
{
    if(!m_pkSingleton) return(0);
    int iParamQuantity = lua_gettop(pkLuaVM);
    if(iParamQuantity<1) {
	printf("Error: SceneManager:Attach() requires 1 parameter\n");
	return(0);
    }
    
    luaL_checktype(pkLuaVM, 1, LUA_TUSERDATA);
    LuaSceneNode *pkLuaSceneNode = (LuaSceneNode*)luaL_checkudata(pkLuaVM, 1, "SceneNode");
    
    if(pkLuaSceneNode) {
	printf("LuaSceneManager: pkLuaSceneNode: 0x%04x\n",(int)pkLuaSceneNode);
	m_pkSingleton->GetScene()->AttachChild(pkLuaSceneNode->GetNode());
    }
    
    lua_pop(pkLuaVM, iParamQuantity);
    return(0);
}


In the above code at Line 14 and in the Lua5 code at the top on Line 3, the memory address of the userdata is printed to the console. The memory address printed from Lua is correct, but the userdata printed from the C++ function isn't. The two memory addresses should be the same, but for whatever reason the member function is either being passed the incorrect userdata from Lua, or the member function is retrieving the userdata argument incorrectly from Lua. Does anybody see anything wrong with the above code/script? Has anybody experienced anything like this? Any ideas of what could be wrong? Everything has been working perfectly until I get to this point. Could it be a problem with Lunar?

Share this post


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

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!