Original Post
Hi I am new to lua. I have encountered a few problems with it. I load Lua State from file, then run it with pcall. The first time it works ok, but any other times it gives error. Here is a code I am using with simple wrapper Here are the errors I am getting when running second and any other number of times: 2) attempt to call a nil value 3) attempt to call a string value Any ideas what I might be doing wrong ?
#include "Lua_File.h"
#include "Tools\Log.h"
LuaState::LuaState()
{
L = NULL;
}
LuaState::~LuaState()
{
Cleanup();
}
void LuaState::Cleanup()
{
if( L != NULL )
{
lua_close(L);
L = NULL;
}
}
//run loaded script
void LuaState::Run()
{
int error;
error = lua_pcall( L, 0, LUA_MULTRET, 0 ); <-- Here is error when runned 2 time
if (error)
{
Log.Error("LUA",-1, __FILE__ , __LINE__ , "LUA failed to run script reason=%s", lua_tostring(L, -1));
}
}
void LuaState::Load(char* filename)
{
FILE *file = fopen( filename ,"r");
L = lua_open(); // opens Lua
if( L == NULL )
{
//lua failed to initialize
Log.Error("LUA",-1, __FILE__ , __LINE__ , "LUA initialization");
return;
}
luaL_openlibs(L);
int error;
error = luaL_loadfile( L , filename );
if (error)
{
Log.Error("LUA",-1, __FILE__ , __LINE__ , "LUA failed to open file %s , reason= %s ", filename , lua_tostring(L, -1));
}
Log.Notice( "File %s loaded" , filename );
fclose(file);
}