lua-users home
lua-l archive

Re: Lua Tutor (Delphi + Lua)

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


Hi, Jilani!
Here I have presented an example somewhat similar to yours. Use LuaTut0.1 found at LuaForge where LuaPas.pas is located at and replace the main function with this example:
function Main(const argc : Integer; const argv : TArgv) : Integer;
var
L : Plua_state;
status : Integer;
res : Double;
begin
L := lua_open(); // create a state
status := luaL_loadfile(L, 'addfunc.lua');
if status = 0 then begin
status := lua_pcall(L, 0, 0, 0);
if status = 0 then begin
// Get the lua function we want to call
lua_getglobal(L, 'add');
// Put the parameters on the stack. In this case the
// two values that will be added.
lua_pushnumber(L, 2.5);
lua_pushnumber(L, 10);
// Call add with 2 parameters, 1 result
status := lua_pcall(L, 2, 1, 0);
if status = 0 then begin
res := lua_tonumber(L, -1);
WriteLn( Format('Add = %0.4g', [res]) );
lua_pop(L, 1);
end;
end
end;
if status <> 0 then
WriteLn('Some error has been occurred!');
lua_close(L);
result := 0;
Write('Hit <Enter> to exit...'); ReadLn;
end;
Add Lua script named 'addfunc.lua' in the folder:
function add(x, y)
return x + y
end
All other functions in the MainUnit should be removed as they are not used.
You could use Delphi 7 to compile the code.
LuaTut was designed for Pascal coders to learn Lua. Since majority of Lua community are C programmers, it is advised to present the code in question in the same manner as the example above so that they can be more helpful to answer.
NOTE: There will be several more examples after I finish the second release for LuaScene Project.
Feel free to ask.
Geo Massar

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