Skip to main content
gamedev.net
🔒 Locked

lua_pcall error when running second time

Started by DMINATOR Feb 26, 2007 at 4:41 PM 1 replies 2.8k views
Original Post
DMINATOR
DMINATOR
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

#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);
}


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 ?
Sneftel
Sneftel
You should read the documentation for lua_pcall, specifically regarding what it pops off the stack.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.