Sign in to follow this  

Lua: read table

This topic is 3593 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'm creating a Lua based ScriptingEngine for our team's game project. i'm using the code at http://www.lua.org/manual/5.1/manual.html#lua_next as you can see the lua_tolstring()-function may not be called which includes the lua_tostring()-macro. so is there any workaround to read string values out of a lua table?

Share this post


Link to post
Share on other sites
Seeing as it says:
Quote:

unless you know that the key is actually a string


You could use lua_isstring().

Alternatively, you could duplicate the object on the top of the stack using lua_pushvalue(L,/* index of key */) and then call lua_tolstring() on the duplicate. Just remember to pop it off to rebalance the stack.

Disclaimer: while I have some experience in manipulating tables in C, I do not claim to be anywhere near an expert. Read the documentation carefully.

Share this post


Link to post
Share on other sites
"Alternatively, you could duplicate the object on the top of the stack using lua_pushvalue(L,/* index of key */) and then call lua_tolstring() on the duplicate. Just remember to pop it off to rebalance the stack."

i tried it but nothing happens

and i do not just want to read the key. i want to get the value too. if its a string or not.

Share this post


Link to post
Share on other sites
I have no idea what you mean by "nothing happens".

This works for meTM

#include <iostream>
#include "lua.hpp"

int test( lua_State *L )
{
int tableindex = lua_gettop(L);
lua_pushnil(L);
while(lua_next(L,tableindex))
{
lua_pushvalue(L,-2);
const char *key = lua_tostring(L,-1);
const char *value = lua_tostring(L,-2);
std::cout << key << " -> " << value << '\n';
lua_pop(L,2);
}
return 0;
}

int main()
{
lua_State *L = luaL_newstate();
lua_register( L, "test", test );
luaL_loadfile(L, "test.lua" );
lua_pcall(L, 0, 0, 0);
}





test.lua:

local table =
{
['foo'] = 3,
['bar'] = 1,
[6] = 'six',
[42.3] = 'almost the answer'
}

test(table)




If this doesn't demonstrate what you are looking for I think you'll have to re-phrase your question.

[edit: corrected minor typo]

[Edited by - rip-off on February 11, 2008 11:00:50 AM]

Share this post


Link to post
Share on other sites
no this doesn't work for me.
look here:


void ScriptEngineArray::_fetchFromLua(lua_State *L) {
this->content.clear();
int table = lua_gettop(L);
lua_pushnil(L);
while(lua_next(L,table) != 0) {
lua_pushvalue(L,-2);
printf("%s [ %s ] - %s\n",
lua_typename(L,lua_type(L,-2)),
lua_tostring(L,-1),
lua_typename(L,lua_type(L,-1))
);
if(lua_isstring(L,-1)) {
const char* key = lua_tostring(L,-1);
lua_pop(L,1);
if(lua_isstring(L,-1)) {
this->content[key] = ScriptEngineVariable::createString(const_cast<char*>(lua_tostring(L,-2)));
} else if(lua_isnumber(L,-1)) {
this->content[key] = ScriptEngineVariable::createInt((int)lua_tonumber(L,-2));
} else if(lua_istable(L,-1)) {
ScriptEngineArray* se_array = new ScriptEngineArray();
se_array->_fetchFromLua(L);
this->content[key] = ScriptEngineVariable::createArray(se_array);
}
}
lua_pop(L,1);
}
}


Made several Classes to handle lua variables. for example ScriptEngineVariable can handle string and int yet and an instance of ScriptEngineArray.

inside lua i got a table with 1 entry.
in c++ i get 2 entries. one is empty and one has the lua's key as key and as value (i'm using map inside the ScriptEngineArray)

hope you can help me

Share this post


Link to post
Share on other sites
Have you tried my code? Can you confirm that it does or does not work?

Also, before you pop "key" from the stack, you should note this:
Quote:

Because Lua has garbage collection, there is no guarantee that the pointer returned by lua_tolstring will be valid after the corresponding value is removed from the stack.


I cannot test your code, I still need more information.

Can you post the rest of your code, or do what I did and make a minimal example that demonstrates the problem you are havin?

Share this post


Link to post
Share on other sites

This topic is 3593 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this