lua-users home
lua-l archive

Re: Lua Tutor (Delphi + Lua)

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Beat me. Suggest to post your code the way I did earlier. Lua folks would be
able to answer. I ain't no pro like them! I just did the implementation.
Like you, I am still learning.
// Call add_xy() from Delphi:
function call_add_xy(L: Plua_State; x, y: Double): Double;
begin
 // Push functions and arguments:
 lua_getglobal(L, 'add_xy'); // Function to be called.
 lua_pushnumber(L ,x); // Push 1st argument.
 lua_pushnumber(L, y); // push 2nd argument.
 // Do the call (2 arguments, 1 result):
 if lua_pcall(L, 2, 1, 0) <> 0 then
 begin
 // Handle error here.
 end;
 // Retrieve result:
 if not lua_isnumber(L, -1) then
 begin
 // Handle error here.
 end;
 Result := lua_tonumber(L, -1);
 // pop returned value:
 lua_pop(L, 1);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
 L: Plua_State;
 FName: PChar;
 x, y, R: double;
 st: string;
 pc: PChar;
begin
 FName := 'exp1.lua';
 L := lua_open();
 luaopen_base(L);
// luaopen_io(L); (*)
 luaL_loadfile(L, FName);
 lua_call(L, 0, 0);
 x := 3.14159;
 y := 2.71828;
 R := call_add_xy(L,x,y);
 st := Format('pi + e = %1.5f', [R]);
 memo1.Lines.Add(st);
 lua_close(L);
end;
exp1.lua file contains:
function add_xy(x, y)
 return (x + y)
end
(*): this line gives an "access violation" if the program is run from Delphi IDE with lua5.1.dll, but not with lua5.0.dll
jk

AltStyle によって変換されたページ (->オリジナル) /