Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get results from a Delphi function called by Lua

Tags:

return

delphi

lua

I am creating a function in Delphi to be able to use it with Lua. I have managed to create a procedure (or a function without taking results) and it works perfect. The problem is that now I want to get results of that function (not a procedure anymore) from Delphi into Lua. I've read some codes out there but couldn't make it work.

My function looks like this:

function TLuaScripter.getsetting(LuaState: TLuaState):TluaState;
var
path,stri: string; i:integer;
begin           //getsetting('Targeting/TargetingEnabled')

 path := Lua_ToString(LuaState, 1); //we get the string 'Targeting/..." 

 Lua_Pop(LuaState,1); //we put it in stack (so we can clean it)

 stri:= tree.GETsetting(path); //this function gets, for example "No"

 lua_pushlstring(LuaState,Pansichar(ansistring(stri)),length(stri)); //I pass it to the lua stack

 //showmessage(Lua_ToString(LuaState,1));--> this shows the result I need in a msgbox

 result:= LuaState;
end;

so now if I use in my Lua Console

getsetting('Targeting/TargetingEnabled')

I am able to get the result in a msgbox (uncommenting the "Showmessage...") but that function doesn't return anything in Lua! If I write

print(getsetting('Targeting/TargetingEnabled'))

I get a Nil value in Lua.

like image 890
user2308704 Avatar asked Nov 12 '22 05:11

user2308704


1 Answers

It will work perfect with this. We have just added the result as integer and "result:=1"

function TLuaScripter.getsetting(LuaState: TLuaState): integer;
var
  path,stri: string;
  i: integer;
begin           //getsetting('Targeting/TargetingEnabled')
  path := Lua_ToString(LuaState, 1);
  stri:= tree.GETsetting(path);
  lua_pushlstring(LuaState,Pansichar(ansistring(stri)),length(stri));
  result:= 1;
end;
like image 200
user2308704 Avatar answered Nov 15 '22 06:11

user2308704