Lua troubles

Started by
4 comments, last by Psilobe 11 years, 6 months ago
I'm learning how to use lua together with c++ but I've run in to some problems where I got an access violation which I cant sort out and after searching and reading I must finally ask for help.

At the bottom at the lua_call I get the access violation and I'm using lua 5.2



[source lang="cpp"]static void spTr(cMap *m,int x, int y)
{
m->spawnTree(x,y);
}

static int spTrWra(lua_State *L)
{
int n = lua_gettop(L);
if(n!=3)
{
lua_pushstring(L, "Wrong number of arguments");
lua_error(L);
}

if(!lua_islightuserdata(L,1))
{
lua_pushstring(L, "wrong data type");
lua_error(L);
}
cMap* M = (cMap*)lua_touserdata(L,1);
int x = lua_tointeger(L,2);
int y = lua_tointeger(L,3);

spTr(M,x,y);

return 0;
}
[/source]
Advertisement
Hi,

your code seems to be cut off and stops after std::cout, so no lua_call to be found :-). Can you post the whole code?
As rnlf said.

However what I see:

In static int spTrWra(lua_State *L) you return 0, indicating that you pushed 0 items on the stack. However the three incoming parameters are still there. You need to pop them off the stack.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Here is the rest of the code, somehow it won t show everything in the op even thou all code is there when I edit.
I found a section about lua in one of my books and it seems I'm going the wrong way with this and combines calling a lua function in c++ while at the same time trying to call a c++ function in lua so I'm currently reading up on the subject.



[source lang="cpp"]cMap::cMap()
{
//should be a std::cout here
int stack = -1;
L = luaL_newstate();
luaL_openlibs(L);
typedef int (*lua_CFunction) (lua_State *L);
lua_register(L, "spawnTree", spTrWra);
init();
luaL_dofile(L,"map.lua");
}

void cMap::init()
{
lua_getglobal(L,"init");
lua_pushlightuserdata(L,this);
const int s = -1;
if(!lua_isfunction(L,s))
{
//another buggy cout
}
const int a = 3;
const int r = 0;
lua_call(L,a,r);
}[/source]

In static int spTrWra(lua_State *L) you return 0, indicating that you pushed 0 items on the stack. However the three incoming parameters are still there. You need to pop them off the stack.


No, you don't need to pop them off the stack. Anything below the results is automatically discarded by Lua.



I found a section about lua in one of my books and it seems I'm going the wrong way with this and combines calling a lua function in c++ while at the same time trying to call a c++ function in lua so I'm currently reading up on the subject.


Your code in spTrWra looks reasonable. However, in the cMap constructor, you call cMap::init, before any Lua code has been run. Therefore, lua_getglobal(L, "init") just pushes nil to the stack. Furthermore, when you call lua_call, you're saying that there are 3 arguments to some function on the top of the stack, presumably the global "init" (which is actually nil here,) but you only push one argument onto the stack, "this."

You probably intend to call luaL_dofile(L, "map.lua") before you call cMap::init, assuming that that is where you define the lua function "init."


I find it helpful to annotate my Lua C API usage, like this:


void cMap::init()
{
lua_getglobal(L,"init"); // nil
lua_pushlightuserdata(L,this); // this nil
const int s = -1;
if(!lua_isfunction(L,s))
{
//another buggy cout
}
const int a = 3;
const int r = 0;
lua_call(L,a,r); //
}


I track the state of the stack in comments next to any functions that modify the stack.

Also, I've found this site to be an invaluable reference.

Between Scylla and Charybdis: First Look <-- The game I'm working on

Object-Oriented Programming Sucks <-- The kind of thing I say

nox_pp I've also found that site when searching for info. Anyway thanks, got it to work after switching place with luaL_dofile() and init() and changed the number of arguments.

I thought there were three arguments since the c++ function I want to call from lua takes three arguments bat the call was for the lua function which only took one. And I needed the lua function to deliver a class pointer as argument when calling my spawn function from lua.

So a big thanks to you! biggrin.png

This topic is closed to new replies.

Advertisement