View Full Version : Using a Lua script in a FreePascal program
Roland Chastain
29-10-2014, 06:14 PM
Hello gentlemen!
I am looking for simple examples demonstrating how to use a Lua script in a FreePascal or Lazarus program. I know that it is possible, because there are important projects (like EGSL (http://www.egsl.retrogamecoding.org//pages/downloads.php?lang=EN)) that use Lua. I look for very basic examples, in the taste of that tutorial (http://www.freebasic-portal.de/tutorials/einbindung-der-skriptsprache-lua-102.html).
I compiled the example included with this unit (http://sourceforge.net/projects/luapascal/?source=directory), but it doesn't do the kind of thing I would like to do. For example I would like to call from my Pascal program a function with a string as argument and get the result of the function.
Would you have some examples to share?
Cybermonkey
30-10-2014, 08:45 PM
What exactly should the function return? Since it has a string as an argument should the string just be changed, chopped or whatever?
(BTW thanks for calling EGSL as an important project ... ;))
Roland Chastain
30-10-2014, 09:15 PM
Thank you for your answer. :)
I would like a function that returns a boolean value. The idea of the function is : "Is the string valid". Here is my Lua function : IsFEN (http://www.developpez.net/forums/d1462841/autres-langages/autres-langages/lua/validation-d-chaine-format-fen/#post7925036).
Cybermonkey
31-10-2014, 03:50 PM
Ok, just for completely understanding: Do you want to execute a Lua script from your Pascal program and return the boolean value to the Pascal program or do you want to implement that function in Pascal and execute it from a Lua script and return the result to the Lua script?
Roland Chastain
31-10-2014, 04:49 PM
Ok, just for completely understanding: Do you want to execute a Lua script from your Pascal program and return the boolean value to the Pascal program or do you want to implement that function in Pascal and execute it from a Lua script and return the result to the Lua script?
To execute a Lua script from my Pascal program and return the boolean value to the Pascal program. In other words, I would like to use my Lua script, from my Pascal program, as I use a Pascal unit or a DLL: call a function and get the result.
Cybermonkey
31-10-2014, 09:25 PM
Ok, I see. That's a bit more complicated than the other way but it's possible. I am using the Lua52 headers which can be found on: http://lua-users.org/wiki/LuaInFreePascal
I'll post a simple example where two integers are added and the sum is returned to the Pascal program, it shouldn't be that hard to adapt it to a string/boolean function.
First the Lua script:
-- add two numbers
function add (x,y)
return (x+y)
end
That's fairly simple. Now the Pascal program (console only). After compiling one has to specify the Lua script as a parameter on the console.
program callLua;
{$mode objfpc}{$H+}
uses sysutils,lua52;
var L : plua_state;
script:string;
sum:integer;
function luaadd (x,y:integer):integer ;
var sum:integer;
begin
//* the function name */
lua_getglobal(L, 'add');
//* the first argument */
lua_pushinteger(L, x);
//* the second argument */
lua_pushinteger(L, y);
//* call the function with 2 arguments, return 1 result */
lua_call(L, 2, 1);
//* get the result */
sum := (lua_tointeger(L, -1));
lua_pop(L, 1);
result:= sum;
end;
begin
if (ParamCount > 0) then
begin
script := pChar(ParamStr(1));
L:=luaL_newstate();
luaL_openlibs(L);
if (luaL_dofile(L,pChar( script)))<>0 then
begin
//show if an error occured
writeln(lua_tostring(L, -1));
end;
sum:=luaadd(12,3 );
lua_close(L);
writeln (sum);
end;
end.
The result should be 15 of course.
Roland Chastain
01-11-2014, 01:40 PM
Thank you very much Cybermonkey!
Very useful example. By modifying it, I could easily make what I needed. :)
Here is my own example (including an Lua script).
Roland Chastain
06-02-2015, 09:09 AM
Hello Cybermonkey!
Here is a new version of your example, using the LUA53.PAS unit from the Lua4Lazarus package.
https://github.com/malcome/Lua4Lazarus
http://forum.lazarus.freepascal.org/index.php/topic,27268.0.html
The Lua script uses a new feature of Lua 5.3 (euclidian division).
Best regards.
Roland
I can get Test Cyp. but not Hcg...am thinking of going with defy...could you share your experiences with using them?Thanks
I am still new to this so maybe I am totally wrong here, but would it not be possible to do something with the Utilities / Command menus ?
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.