Results 1 to 10 of 10

Thread: Using a Lua script in a FreePascal program

  1. #1

    Using a Lua script in a FreePascal program

    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) that use Lua. I look for very basic examples, in the taste of that tutorial.

    I compiled the example included with this unit, 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?

  2. #2
    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 ... )
    Best regards,
    Cybermonkey

    Pulsar2D framework:
    http://pulsar2d.org

  3. #3
    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.
    Last edited by Roland Chastain; 30-10-2014 at 08:21 PM. Reason: Added a link to a Lua script

  4. #4
    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?
    Best regards,
    Cybermonkey

    Pulsar2D framework:
    http://pulsar2d.org

  5. #5
    Quote Originally Posted by Cybermonkey View Post
    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.

  6. #6
    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:
    Code:
    -- 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.
    Code:
    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.
    Best regards,
    Cybermonkey

    Pulsar2D framework:
    http://pulsar2d.org

  7. #7
    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).
    Attached Files Attached Files
    Last edited by Roland Chastain; 01-11-2014 at 12:55 PM.

  8. #8
    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/...c,27268.0.html

    The Lua script uses a new feature of Lua 5.3 (euclidian division).

    Best regards.

    Roland
    Attached Files Attached Files

  9. #9

    Using a Lua script in a FreePascal program

    I can get Test Cyp. but not Hcg...am thinking of going with defy...could you share your experiences with using them?Thanks

  10. #10

    Using a Lua script in a FreePascal program

    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 ?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Comodo SSL