I got 2 functions that register Lua table and method in C++:
void LuaScriptInterface::registerTable(const std::string& tableName)
{
// _G[tableName] = {}
lua_newtable(luaState);
lua_setglobal(luaState, tableName.c_str());
}
void LuaScriptInterface::registerMethod(const std::string& globalName, const std::string& methodName, lua_CFunction func)
{
// globalName.methodName = func
lua_getglobal(luaState, globalName.c_str());
lua_pushcfunction(luaState, func);
lua_setfield(luaState, -2, methodName.c_str());
// pop globalName
lua_pop(luaState, 1);
}
It registers some methods:
registerTable("Game");
// Game.getHouses()
registerMethod("Game", "getHouses", LuaScriptInterface::luaGameGetHouses);
Then I call in Lua:
local param = "print( Game.getHouses() )"
pcall(loadstring(param))
I got problem with param. Calls and results:
1. print(Game.getHouses())
2. print(Game['getHouses']())
3. print( Game.getHouses() ) -- added spaces
4. print( Game['getHouses']() ) -- added spaces
5. local var = Game.getHouses() print(#var)
6. local var = Game['getHouses']() print(#var)
7. local var = #Game.getHouses() print(var)
8. local var = #Game['getHouses']() print(var)
9. local var = # Game.getHouses() print(var) -- added space
Results:
1. attempt to call a nil value
2. table: 0x4351fdd0
3. table: 0x42ce6b88
4. table: 0x426513c0
5. 1010
6. 1010
7. attempt to call a nil value
8. 1010
9. 1010
Can anyone tell me a reason, why it does not work in loadstring/pcall?
Can I make it work in loadstring/pcall somehow?
EDIT:
After 2 hours of debugging. I found out, that client that I use to communicate with server - that executes LUA - does some regex on string that I send (I still don't know why, but it's not LUA related) :)