@richard
Вы можете использовать функцию TMemo.Lines.Add() для добавления строк в TMemo. Например:
1 2 |
local result = RunYourLuaScript() memo:Lines.Add(result) |
Где "memo" - это имя вашего TMemo компонента, "RunYourLuaScript" - функция, которая выполняет ваш скрипт Lua и возвращает результат.
@richard
Вот пример кода на Delphi, который выполняет скрипт Lua и выводит его результат в TMemo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
procedure TForm1.Button1Click(Sender: TObject);
var
L: Plua_State;
ResultCode: Integer;
begin
L := luaL_newstate;
luaL_openlibs(L);
ResultCode := luaL_dostring(L, 'print("Hello, Lua!")');
if ResultCode <> 0 then
begin
ShowMessage('Error executing Lua script');
Exit;
end;
lua_close(L);
end;
procedure TForm1.ExecuteLuaScript(const Script: string);
var
Output: TStringList;
L: Plua_State;
ResultCode: Integer;
begin
Output := TStringList.Create;
try
L := luaL_newstate;
luaL_openlibs(L);
ResultCode := luaL_loadstring(L, PAnsiChar(AnsiString(Script)));
if ResultCode = 0 then
begin
ResultCode := lua_pcall(L, 0, LUA_MULTRET, 0);
if ResultCode = 0 then
begin
GetLuaStack(L, Output);
memo.Lines.AddStrings(Output);
end
else
begin
ShowMessage('Error executing Lua script');
end;
end
else
begin
ShowMessage('Error loading Lua script');
end;
lua_close(L);
finally
Output.Free;
end;
end;
procedure TForm1.GetLuaStack(L: Plua_State; Output: TStrings);
var
Depth: Integer;
ValueType: Integer;
begin
Depth := 0;
while True do
begin
ValueType := lua_type(L, -1 - Depth);
if ValueType = LUA_TNONE then
Break;
case ValueType of
LUA_TNIL:
Output.Add('nil');
LUA_TBOOLEAN:
begin
if lua_toboolean(L, -1 - Depth) then
Output.Add('true')
else
Output.Add('false');
end;
LUA_TLIGHTUSERDATA:
Output.Add('lightuserdata');
LUA_TNUMBER:
Output.Add(FloatToStr(lua_tonumber(L, -1 - Depth)));
LUA_TSTRING:
Output.Add(lua_tostring(L, -1 - Depth));
LUA_TTABLE:
Output.Add('table');
LUA_TFUNCTION:
Output.Add('function');
LUA_TUSERDATA:
Output.Add('userdata');
LUA_TTHREAD:
Output.Add('thread');
LUA_TNONE:
Output.Add('none');
end;
Inc(Depth);
end;
end;
|
В этом примере кнопка Button1.onClick выполняет скрипт Lua (print("Hello, Lua!")), а результат выводится в TMemo (memo).