LUA Custom Method Problem

ludovicV

New member
Joined
Feb 12, 2013
Messages
2
Reaction score
0
Points
0
Hey guys!

I'm developing a module that allows you to put a fixed camera on an object. It's mainly for taking videos of launch (to get the same as a real launch kind of footage). Thus, I want to interface it with Lua script. It works fine without the Lua interface ;)

The module basically allows you to create a camera vessel (just like FlyByOrbiter)

I followed the doc and created these methods to get the Lua's interface:
Code:
int CAMERA::clbkGeneric (int msgid, int prm, void *context)
{
  switch (msgid) {
  case VMSG_LUAINSTANCE:
    return Lua_InitInstance (context);
  }
  return 0;
}


Code:
int CAMERA::Lua_InitInstance (void *context)
{
  lua_State *L = (lua_State*)context;

	// check if interpreter has DG table loaded already
	luaL_getmetatable (L, "VESSEL.CAMERA");

	if (lua_isnil (L, -1)) { // register new functions
		lua_pop (L, 1);
		static const struct luaL_reg dgLib[] = {
			{"setTargetCamera", setTargetCamera},
			{NULL, NULL}
		};

		// create metatable for vessel userdata
		luaL_newmetatable (L, "CAMERA.vtable");

		// create a table for the overloaded methods
		luaL_openlib (L, "CAMERA.method", dgLib, 0);

		// create metatable for accessing inherited methods from VESSEL
		luaL_newmetatable (L, "CAMERA.base");
		lua_pushstring (L, "__index");
		luaL_getmetatable (L, "VESSEL.vtable");
		lua_settable (L, -3);

		// set DG.base as metatable for CAMERA.method
		lua_setmetatable (L, -2);

		// point vessel userdata to CAMERA.method
		lua_pushstring (L, "__index");
		lua_pushvalue (L, -2); // push CAMERA.method
		lua_settable (L, -4);

		// pop CAMERA.method from the stack
		lua_pop (L, 1);
	}

	lua_setmetatable (L, -2);

	return 0;
}

and:
Code:
static int setTargetCamera (lua_State *L)
{
  return 0;	
}

Now, when i run in the Lua console:
Code:
camAtlas=vessel.get_interface('cameraAtlasV')
camAtlas:setTargetCamera()

I get "Execution Error"... I tried camAtlas:get_name() and it works just fine, so I've no idea of what's wrong with my piece of code. I run out of ideas, can you help me?

Thanks in advance,
Ludovic
 
Shouldn't it be
Code:
camAtlas.setTargetCamera()
?

I am not sure if the push-pop operations make sense, but I would need more time than my lunch break to help you.
 
Urwumpe is write about the semi-colon. Writing camAtlas:setTargetCamera() is equivalent to camAtlas.setTargetCamera(camAtlas), which is not what you want (at least, for now). However, I don't think that the program would crash because of that. But I can't find what is going wrong... Maybe because you didn't pop the self (because you used the semi-colon), Lua is freaking out, but as far as I remember, this should not cause any problem.
 
Thanks for the quick replies :)

I tried without the semi-column, and I still have the same error. I'm gonna continue to investigate ;)

---------- Post added at 03:50 PM ---------- Previous post was at 02:21 PM ----------

And it's getting even better now... I've exactly the same code as before but Orbiter crash after loading the module except if I comment all the part related to LUA...

---------- Post added at 07:18 PM ---------- Previous post was at 03:50 PM ----------

Apparently, I've another problem. I'm trying to make the script change the camera focused object. When i call oapi.set_cameratarget(camAtlas.get_handle(),2), orbiter crashes. Any ideas?
Edit:nevermind, it's camAtlas:get_handle()... I thought i tried both of them
 
Last edited:
According to my Lua book, camAtlas:get_handle() makes no sense at all.

It would be translated to: camAtlas.get_handle(camAtlas) and then translated to get_handle(camAtlas, camAtlas)
 
Last edited:
Back
Top