LUA Passing a variable from C++ to Lua and..

JMW

Aspiring Addon Developer
Joined
Aug 5, 2008
Messages
670
Reaction score
89
Points
43
Location
Happy Wherever
Someone please put me out of my misery.

How do I call a Lua function from C++ ? and...
Specifically, I want to pass my variable "altm" into the stock Lua Script "aap.lua" value "alt", from my routine in .dll
I've read multiple articles, cut n pasted for hours, but to no avail.
Does this resemble anything that would do it?
Code:
int ShuttlePB::clbkGeneric (int msgid, int prm, void *context)
{
switch (msgid) {
case VMSG_LUAINTERPRETER:
	lua_State *L = (lua_State*)context;
	
luaL_dofile (L, "Script\\35B\\aap.lua");
lua_pcall(L, 0, LUA_MULTRET, 0);

lua_getglobal(L, "alt_ap (alt)");
lua_pushnumber(L, 500);   // altm <---THE VALUE I WANT TO PASS
lua_pcall(L, 1, 1, 0);
lua_pop(L,1);
    
return VMSG_LUAINTERPRETER;
	}
    return 0;
}

Showing my ignorance of Lua (and other things) does the function alt_ap have to be called as well or does it run when aap.lua is loaded? If so, how?
:sos:
 
Someone please put me out of my misery.

For calling functions from C...

First you need to push the function reference on the stack, then the arguments. Use no signature for the function, only the name alt_ap should be enough in Lua.


Everything looks right there.

Luckily I have a Lua book from my old university days for such problems...
 
Last edited:
Thanks Urwumpe but it doesn't work...........
Where else can I look?
I don't have to change the script file "aap" do I? :hmm:
 
Have you tested if the aap.lua has been loaded into the context and the functions are available?
 
Well the script "warning" text appears in the Terminal mfd and it works.
How can I tell if the functions are available?
 
I don't know if a such simple advice would help you, but if I were you I´d edit the aap.lua inserting some debug lines such as..

--********DEBDEBDEB**********
oapi.dbg_out('The value of my variable is now:'..myvariable)
--********DEBDEBDEB**********

or...

function myfunction(itsvariable)

--********DEBDEBDEB**********
oapi.dbg_out('I have entered myfunction now!')
--********DEBDEBDEB**********
.
.
.
end



If things get more complicated and you need more report lines you can use a debug report file instead of oapi.dbg_out().

Rgds

Beep
 
  • Like
Reactions: JMW
Back
Top