lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello!  I'm writing a game engine based on preexisting data files, and some of them are Lua scripts.  I'm trying to run them to initialize some data in the program using function calls, but strangely only the first line of the script seems to be running.

The script code looks something like this:
[code]
SetTileFlags(0, {"water"})
SetTileFlags(1, {"water"})
SetTileFlags(2, {"water"})
SetTileFlags(3, {"water"})
SetTileFlags(4, {"water"})
SetTileFlags(5, {"water"})
SetTileFlags(6, {"water"})
SetTileFlags(7, {"water"})
--etc...
[/code]

As you can see, it calls the same function over and over again with different parameters.

I've written the following function in C++:
[code]
int luaConfigure(lua_State* L) {
	std::cout << "Lua config function called!" << std::endl;


	double d = (double)luaL_checknumber(L, 1);


	luaL_checktype(L, 2, LUA_TTABLE);
	lua_rawgeti(L, 2, 1);

	const char* s = lua_tostring(L, -1);
 
	std::cout << d << std::endl << s << std::endl;

        //to restore the stack to its original state,
        //but I get the same results even if I don't
        //do this.
	lua_pop(L, 3);
}
[/code]

If the script is hooked up correctly, I would expect the output to be:
[code]
Lua config function called!
0
water
Lua config function called!
1
water
Lua config function called!
2
water
Lua config function called!
3
water
(etc...)
[/code]

This is (with some simplification) the code I use to
set it up:

[code]
	lua_State* L = lua_open();

	lua_pushcfunction(L, luaConfigure);
    	lua_setglobal(L, "SetTileFlags");

        if (luaL_loadfile(L, configfile.c_str()) )
		std::cout << "trouble loading lua script." << std::endl;

	if (lua_pcall(L, 0, 0, 0) )
  	      std::cout << "Trouble when running script: " << lua_tostring(L, -1);

	lua_close(L);
[/code]

This code for running the script is based of an example in the book "Programming in Lua".

At any rate, when I run it, the output I get is this:
[code]
Lua config function called!
0
water
[/code]

So it looks like it only called the function once.  To confirm this, I commented out the first few likes in the Lua file, and in general it only makes the first function call.

I looked at the API reference, and found a function called luaL_dofile() that is supposed to run the entire file.  But strangely, my compiler complained that it wasn't declared in the header file when I tried to use it.

The documentation says that this function is defined by the following macro:
[code]
     (luaL_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0))
[/code]
Which is more or less what I did, except that it has an LUA_MULTRET parameter where I put zero.  So I tried changing my code to look like this, and this time, not only did it only call the function once, but it also resulted in a program crash (Segmentation fault (core dumped)).

So I'm at a bit of a loss as to what to do.  Any light anyone can shed would be appreciated.

Thanks,
Cooper
_________________________________________________________________
Introducing Live Search cashback .  It's search that pays you back!
http://search.live.com/cashback/?&pkw=form=MIJAAF/publ=HMTGL/crea=introsrchcashback