# 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.

## 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 on other sites
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

1. 1
Rutin
23
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 9
• 33
• 41
• 23
• 13
• ### Forum Statistics

• Total Topics
631745
• Total Posts
3002005
×