Lua and lua_pcall problems

ChrisS
Unregistered
 
Post: #1
I just started trying ot learn Lua to add some scripts into my newest game. Things were going smoothly until I tried to call some Lua functions from my program. Everytime I try to call my Lua function I get a "attempt to call a nil value" which unfortunately isn't telling me much. I'm hoping someone can tell me where I'm going wrong.

Code:
lua_State *L;

void InitLua(void)
{
/*** Starting Lua ******/
L = lua_open();                 /* opens Lua */
if(L!=nil)
    {
    luaopen_base(L);             /* opens the basic library */
    luaopen_table(L);            /* opens the table library */
    luaopen_io(L);               /* opens the I/O library */
    luaopen_string(L);           /* opens the string lib. */
    luaopen_math(L);             /* opens the math lib. */
    
    lua_register(L, "GetSquareValue", GetSquareValue);
    lua_dofile(L, "Map.lua");
    }
/**********************/
}

void InitLuaMap(void)
{
char errstr[50];

/* push functions and arguments */
lua_getglobal(L, "InitMainMap");  /* function to be called */
lua_pushnumber(L, MAP_HEIGHT);   /* push 1st argument */
lua_pushnumber(L, MAP_WIDTH);   /* push 2nd argument */

if (lua_pcall(L, 2, 0, 0) !=0)
    {
    strcpy(errstr,(char *)lua_tostring(L, -1));
    Error(errstr);
    }
}


Any help you could give me would be much appreciated. Thanks!
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #2
Try like this:
Code:
lua_pushstring(L, "InitMainMap");
    lua_gettable(L, LUA_GLOBALSINDEX);

also check if lua_dofile is giving an error.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Member
Posts: 184
Joined: 2004.07
Post: #3
What you're doing actually looks okay, so there might be something wrong with Map.lua. You can use lua_isfunction() to make sure that a function is in fact being retrieved from your global, and to get a better idea what's going on you can install a panic function (lua_atpanic) to figure out what line is crapping out if any during runtime.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  lua_pcall returns a value 2 knowledge_seeker 3 7,082 Mar 16, 2012 06:05 PM
Last Post: Oddity007