[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Problem with table created from C
- From: Steve Heller <steve@...>
- Date: Mon, 11 Sep 2006 14:18:38 -0700 (PDT)
Hi all,
I'm having a problem when creating a Lua table from
C++, then asking for its value later. For some reason,
the table doesn't seem to exist.
Code that shows what I'm doing is at the bottom of
this message. The
"GetThreadLuaPointer"/"SetThreadLuaPointer" functions
use thread local storage to save the Lua environment
pointer. There are two phases of this program.
1. Store the rules into the RuleManager table, via
code like this:
LuaRuleManager RulesObject;
LuaRuleManager.AddRule(ruleNumber,ruleString);
2. Retrieve the rules from the RuleManager table, via
code like this:
LuaRuleManager RulesObject;
string result = LuaRuleManager.GetRule(ruleNumber);
The storage part works fine; I've confirmed that the
rules are actually being saved in the RuleManager
table. Unfortunately, when I try to retrieve the
rules, the RuleManager object comes up as nil.
Any suggestions as to what the problem may be, or how
to fix it?
Thanks.
--------------------------------
LuaRuleManager::LuaRuleManager()
{
m_L = GetThreadLuaPointer();
if (m_L == 0)
{
m_L = lua_open(); /* opens Lua */
SetThreadLuaPointer(m_L);
luaL_openlibs(m_L); /* open
standard libraries */
lua_newtable(m_L); // create new
table
lua_setglobal(m_L,"RuleManager"); // assign
it to global variable named RuleManager
}
}
LuaRuleManager::~LuaRuleManager()
{
}
void LuaRuleManager::AddRule(int id, const string&
RuleString)
{
lua_getglobal(m_L,"RuleManager");
if (!lua_istable(m_L,-1))
__asm int 3; //RuleManager table not found
lua_pushnumber(m_L,id);
lua_pushstring(m_L,RuleString.c_str());
lua_settable(m_L,-3);
lua_settop (m_L, 0);
}
string LuaRuleManager::GetRule(int id)
{
lua_getglobal(m_L,"RuleManager");
if (!lua_istable(m_L,-1))
{
PassThrough::DumpStack(m_L);
__asm int 3; //RuleManager table not found
}
lua_pushnumber(m_L,id);
lua_gettable(m_L,-2);
size_t size;
const char* rule = lua_tolstring(m_L,-1,&size);
lua_settop (m_L, 0);
return string(rule,size);
}