5

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.

7
  • 2
    You should register a function of type lua_CFunction which returns number of results pushed into stack. So, change return type to Integer and properly set the returning value result:=1;. BTW, cleaning the stack by lua_pop is optional. Commented May 12, 2013 at 22:46
  • 1
    Example of proper function declaration: function getsetting(L: Plua_State): Integer; cdecl; Commented May 12, 2013 at 22:49
  • I've done the following: -Changed return type from TLuaState to Integer. -Changed result value to 1. -Also I have deleted the lua_pop as you said It is working now just like that, but it didn't work with the "cdecl" in the function. Why is it needed? And about the "lua_CFunction", why do we need this if I can actually print the value like "print(getsetting(...))"? Thanks for the help. Commented May 13, 2013 at 11:48
  • Only functions conformed to lua_CFunction are possible to be available from Lua code. Please show your code where you are registering your function TLuaScripter.getsetting. Commented May 13, 2013 at 14:19
  • no problem, it's okay since now it's working properly. A lot of thanks! Commented May 13, 2013 at 14:58

1 Answer 1

1

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; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.