Call CELX methods from a C++ module running inside Celestia
Posted: 08.10.2008, 08:18
Hi all,
I am doing some experiment on Celx-scripts, I’m new to lua programming and I have a few questions about it.
Actually here is what I’m trying to do: I’d like to develop a C++ module which can interact with Celestia through Celx-scripts.
Therefore I built a lua module made in C++ with a Qt interface that I can call from a Celx-script. When Celestia “ticks” the script it replaces the main loop of my module:
I wrapped the two functions qtTest.tick() and qtTest.init() with Swig and it works well.
Now I would like to call Celx-methods from my module. First, when I initialized my module with luaopen_qtTest (lua_State* L) I stored the lua state for future use. Then for example I’d like to call "celestia:gettimescale" lua method from my C++ code, but I don’t know how to do this. Here is what I try to do:
Actually I'm sure that lua_getglobal isn't the right way to do, I made different try but for the moment all my tests end with a crash of Celestia when I try to lua_call a function… I don’t exceed in lua yet and I wonder if I have to get the "celestia" object first and then call the gettimescale function on it, but I don’t know the way to do it. I had a lookwithin the source code of Celestia, I saw the CelxLua::ClassNames array, so I'm looking this way. Would you help me understand how this great engine works?
Cheers,
Johan (SpaceBel)
I am doing some experiment on Celx-scripts, I’m new to lua programming and I have a few questions about it.
Actually here is what I’m trying to do: I’d like to develop a C++ module which can interact with Celestia through Celx-scripts.
Therefore I built a lua module made in C++ with a Qt interface that I can call from a Celx-script. When Celestia “ticks” the script it replaces the main loop of my module:
Code: Select all
require("qtTest")
hooks = {}
function hooks:tick( dt )
assert(qtTest.tick)
time = celestia:gettime()
qtTest.tick(time)
end
--------------------
-- register hooks
--------------------
celestia:setluahook( hooks )
--------------------
-- init
--------------------
assert(qtTest.init)
qtTest.init(1,nil)
I wrapped the two functions qtTest.tick() and qtTest.init() with Swig and it works well.
Now I would like to call Celx-methods from my module. First, when I initialized my module with luaopen_qtTest (lua_State* L) I stored the lua state for future use. Then for example I’d like to call "celestia:gettimescale" lua method from my C++ code, but I don’t know how to do this. Here is what I try to do:
Code: Select all
double timeScale;
lua_getglobal(L, "celestia:gettimescale");
lua_call(L,0,0);
timeScale = (double)lua_tonumber(L, -1);
Actually I'm sure that lua_getglobal isn't the right way to do, I made different try but for the moment all my tests end with a crash of Celestia when I try to lua_call a function… I don’t exceed in lua yet and I wonder if I have to get the "celestia" object first and then call the gettimescale function on it, but I don’t know the way to do it. I had a lookwithin the source code of Celestia, I saw the CelxLua::ClassNames array, so I'm looking this way. Would you help me understand how this great engine works?
Cheers,
Johan (SpaceBel)