Sign in to follow this  
BradDaBug

Problem calling Lua script function

Recommended Posts

I'm trying to call a function down inside a Lua script. Here's the code:
luaL_loadbuffer(CScripter::state, script, scriptLength, filename.c_str());
		lua_getglobal(CScripter::state, functionname.c_str());
		

		CScripter::lang_PushPointer(param); // just a wrapper around lua_newuserdata() (mostly, anyway)
		
		int err = lua_pcall(CScripter::state, 1, 0,0);

		if (err != 0)
		{
			const char *errmessage = CScripter::lang_GetStringFromStack(-1);
			DEBUG_OUTPUT("Error: " << errmessage);
		}

The error message that I get each time I run this code is "Error: attempt to call a nil value". It looks like all the parameters I'm giving to lua_getglobal() are good. 'script' is a valid buffer containing a script, 'scriptLength' contains the length of 'script', 'filename' is just the name of the script. What am I doing wrong?

Share this post


Link to post
Share on other sites
Well, according to the error it means that the function you are calling is not in the global namespace. It's either inside a different table, or it doesnt exist. From your pcall your function has to take 0 arguments as well.

The only problem I see is that you are pushing the function name onto the stack, then apparently pushing a userdata on top of it. Then you are calling lua_pcall saying that there is 1 argument. If for whatever reason the userdata isn't on top of the stack, its going to treat the function as the argument, and the nothing below it as the function call, which would give you the error you are seeing.

Ahh, I think I figured it out. You are calling lua_loadbuffer which loads a script but doesnt run it and leaves it on the stack. You need to call lua_call after calling loadbuffer *or* you need to call lua_dobuffer instead, which does a loadbuffer, then a call. You have to run a script in order to "register" the functions in it with the lua registry.

Share this post


Link to post
Share on other sites
O man, whenever I do "print(anything);" my program crashes. I don't know if it's always been like that or it just started since I never really put print() in the startup script (which works except when I put a print() in it).

What would make Lua crash whenever I use print?

Edit: It looks like the Lua IO, standard, and math libraries are failing to load. The base lib is working, though.

Share this post


Link to post
Share on other sites
When you are imbedding the lua interperator into your application the library doesnt have the print function embedded because you may not be making a console application. Just create a c function, and bind it into lua as print.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this