Results 1 to 5 of 5

Thread: Combining Lua and Free Pascal.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #5
    Sorry for the late reply. I ended giving up on the project due to the lack of support overall, and because I simply couldnt do with freepascal at the time I could do with Borland Pascal. Main reason being the lack of a crt unit after a fresh install of Free Pascal.

    This is the test pascal file that is working as of today, reorganized the register functions to above the main.lua file being loaded.

    Code:
    program lua_test;
    
    uses
            crt,
            lua,
            lualib,
            lauxlib;
    
    var
            L: plua_state;
    {--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--}
    function new_print(L: Plua_State): LongInt; cdecl;
    {
        Redefined print here as it causes problems when using freepascal with Lua.
        Executing code from fpc, and using print in lua results in the cursor not
    going back to the beginning.
    }
    var
            vars:string;
            i:integer;
    
    begin
            vars:='';
            for i:=1 to lua_gettop(L) do begin
                    if i = 1 then vars:=vars + lua_tostring(L, i) else vars:=vars + '    ' + lua_tostring(L, i);
            end;
    
            writeln(vars);
            new_print:=0;
    end;
    {--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--}
    function cls(L: Plua_State): LongInt; cdecl;
    begin
            clrscr();
            cls:=0;
    end;
    {--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--}
    procedure SendtoLua(); cdecl;
    begin
            writeln('===================');
            lua_getglobal(L, 'luatest');
            lua_pushinteger(L, 5);
            lua_pushstring(L, 'r2wea');
            lua_pushboolean(L, true);
            {   call:   x args, y result }
            lua_call(L, 3     , 4        );
            writeln ('Pascal received: ', lua_tointeger(L, -4),', ', lua_tostring(L, -3),', ', lua_tointeger(L, -2), ', ', lua_toboolean(L, -1));
            writeln('===================');
    end;
    {--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--}
    begin
            clrscr;
            L := luaL_newstate();
    
            {check if the lua engine is loaded.}
            if (L = nil) then begin
                    writeln('Lua engine not loaded! Terminating!');
                    exit;
            end else begin
                    writeln('Step 1: Loading Lua engine.');
                    luaL_openlibs(L);
    
                    {Register freepascal functions and procedures before loading the lua file.}
                    lua_register(L, pchar('cls'), @cls);
                    lua_register(L, pchar('new_print'), @new_print);
                    if lua_dofile(L, pChar('main.lua')) <> 0 then begin
                            Write('Error Detected: ');
                            WriteLn(lua_tostring(L, -1));
                            exit;
                    end;
            end;
    
            {Check if the main.lua file exists, and executes it.}
            writeln('Step 3: Loaded, and Executed the lua script.');
    {=======================================================================================================================
    Main code for execution goes here.
    =======================================================================================================================}
            SendtoLua();
    
            lua_close(L);
    end.

    The main.lua file is here.
    Code:
    --oprint = print -- Saving original print
    --print = new_print -- Redefinging print
    asd={}
    asd.d=1
    asd.e=3
    
    function luatest(aaa,bbb,ccc)
        print('calling lua test')
        print('Lua received: ' .. aaa .. ', ' .. bbb .. ', ' .. type(ccc) .. '.\n')
    
        if ccc == true then
            print('this is true')
        else
            print('this is false')
        end
    
        print('Alpha.')
        print("Beta.")
        return 99,"a77",43, asd
    end
    
    --cls()
    new_print('Test line.')
    print('Step 2: Loaded main.lua. ')
    print('Testing.')


    Its not the cleanest project, i'll admit. As it was meant as a proof of concept for me. Since freepascal does ship with Lua 5.1 (which i do need above the latest versions), this is what I'll be using myself. I'll do some cleanup, see if i can get the project to do what i need it to do, and see what direction i can take it in terms of a possible console game.
    Last edited by Dilan Rona; 21-03-2023 at 09:43 AM. Reason: Cleaning up the post and placing code inside proper tags

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
  •