Jump to content
  • Advertisement
Sign in to follow this  
McFury

Lua table exposing issues

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

Hi, I didn't know if this was the right place to ask (I'm hoping someone knows lua :P), but i'm having having trouble trying to get table entries (declared in lua) from cpp code. I know its possible so its most likely bad syntax on my part. i'll just paste the code and scripts i'm using. (TestScene.lua)
uniquename = {}
resourcename = {}
size = {}
type = {}
textures = {}
detail = {}
    
function TerrainEntry(b) 
	
	uniquename[b.uniquename] = true
	resourcename[b.resourcename] = true 
	size[b.size] = true 
	type[b.type] = true 
	textures[b.textures] = true
	detail[b.detail] = true
end
   
dofile("TestSceneData.lua")
(TestSceneData.lua)
TerrainEntry
{
     	uniquename = "Terrain-1",
      	resourcename = "Resources/Terrains/height128.raw",
      	size = 128,
      	type = 0,
	textures= {"Resources/Materials/new1.tga","Resources/Materials/new2.tga"},
	detail = "Resources/Materials/DetailMap1.tga"
}
I know that the above scripts work, because i've tested printing out the values after i call "dofile" and they display correctly. The problem im having comes when I try to expose these values to the stack so i can access them from cpp. (inside cpp functions)
void LoadFile(char *fileName)
{
	m_scriptor->RunScriptFile("TestScene.lua"); //Hard-coded for now

	std::string temp = m_scriptor->GetTableString("uniquename");
}
(The above 'GetTableString' function calls this code below, where 'key' is the char* "uniquename")
...
	lua_pushstring(L, key);

	lua_gettable(L, -2);

	if(!lua_isstring(L, -1))
	{
		std::string err("<LuaPopStringFieldFromTable> Cannot retrieve: ");

		//throw std::runtime_error(err + key);
	}

	std::string s = lua_tostring(L, -1);

	lua_pop(L, 1);   // <-------- IT DIES HERE

	return s;
...
If anyone could be of any help i would be very thankful, im so confused.

Share this post


Link to post
Share on other sites
Advertisement
don't have lua refman around here, but you must do something like:

lua_push(L, "tablename");
lua_gettable(L,LUAXGLOBALINDEX);
lua_push(L,"indexname");
lua_gettable(L,-2);

you should get the result on top of the stack

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.

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!