Jump to content
  • Advertisement
Sign in to follow this  

Issues with Lua embed in C

This topic is 2163 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm implementing a scripting system in a project, and I decided to go with Lua. All was fun and games until I started trying to pass a pointer for the entity along to the script, in order for my bound C funcs to be able to manipulate the entity. However, I can't seem to pop my value off the stack once my bound function is called no matter what:



Script run function:

bool anne_script_run(ANNE_SCRIPT_UNIT *unit){
	lua_pushlightuserdata(unit->machine, unit->entity);
	if (luaL_dostring(unit->machine, unit->script)){
		// TODO: need to pass script file info for more detailed errors
		fprintf(stderr, "Failed to execute script: %s\n", lua_tostring(unit->machine, 1));
		return false;
	} else {
		char const *result = lua_tostring(unit->machine, 2);
		printf("result: %s\n", result);
		return true;

Bound API function:

int anne_script_api_get_ent_var(lua_State *machine){
	int argc = lua_gettop(machine);

	// Get the pointer to our entity back out
	int i;
	ANNE_ENTITY *entity = NULL;
	for (i = 1; i <= 20; i++) {
		entity = lua_touserdata(machine, i);
		if (entity != NULL) {
			printf("Y: stack number is %i\n", i);
			return 1;
		} else {
			printf("N: stack number is %i\n", i);
	return 0;

I'm quickly approaching abject bewilderment. If I replace the luaL_dostring() call with the longform:

luaL_loadstring(unit->machine, unit->script);
	lua_getfield(unit->machine, LUA_GLOBALSINDEX, "main");
	lua_pushlightuserdata(unit->machine, (void *) unit->entity);
	if (lua_pcall(unit->machine, 1, LUA_MULTRET, 0)){

then my console spits out:

Failed to execute script: attempt to call a nil value

and it doesn't appear to really matter what's inside my script at that point, though for posterity the contents of every.lua are:

function main(entity)
	return "boffins"

If anyone could steer me in the right direction, I'd really appreciate it. I've been staring at this for four hours and the walls are beginning to melt as if I'd been huffing some particularly dire narcotics (spoiler: I haven't.)

Edited by Winfield

Share this post

Link to post
Share on other sites

Update, some time later: Had to unpack the variable passed inside the script:

local entity = ...

and pass the lightuserdata construct to my C function like so: 

local myX	= anneGetEntityData(entity, "x")

All's well that ends, I suppose, but wow did the available documentation not make this explicit.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!