Page 4 of 5 FirstFirst ... 2345 LastLast
Results 31 to 40 of 42

Thread: Getting started with LUA/pas2lua

  1. #31

    Getting started with LUA/pas2lua

    Quote Originally Posted by Robert Kosek
    Quote Originally Posted by Delfi
    {$mode delphi} // fixes this
    I had mode Delphi defined at the top of the unit. It didn't fix that.
    it has to be defined prior the "unit xyz" part.
    This is my game project - Top Down City:
    http://www.pascalgamedevelopment.com...y-Topic-Reboot

    My OpenAL audio wrapper with Intelligent Source Manager to use unlimited:
    http://www.pascalgamedevelopment.com...source+manager

  2. #32

    Getting started with LUA/pas2lua

    Oh, I'd have to go back and double check that, thanks for the tip. Though that's the least of my problems currently; I'm having trouble passing a function as an argument (Pointer style) that is making the program choke.

  3. #33

    Getting started with LUA/pas2lua

    Working with functions as parameters isn't near as easy as it sounds. You have to keep a reference to the function in the Lua Registry, and then you have to use that entry to actually call the function.

    The following is a "simple" sample:[pascal]function lua_CallFunc(L : PLua_State) : Integer; cdecl;
    var
    n, callTimes, funcRef, c, pidx : Integer;
    pArray : array of variant;
    begin
    n := lua_gettop(L); // Get the number of items on the stack
    callTimes := lua_tointeger(L, 1); // Get the number of times to call the function
    if lua_type(L, 2)=LUA_TFUNCTION then // Make sure we have a function
    begin
    lua_pushvalue(L, 2); // Make a copy to push to the registry
    funcRef := luaL_ref(L, LUA_REGISTRYINDEX); // push to the registry and get the reference
    try
    SetLength(pArray, n-3); // Get the params that we are passing
    for c := 3 to n do
    pArray[c-3] := luatovariant(L, c);
    for c := 1 to callTimes do
    begin
    lua_rawgeti(L, LUA_REGISTRYINDEX, funcRef); // Place the reference on the stack
    for pidx := 0 to length(pArray)-1 do // place each param on the stack
    luapushvariant(L, pArray[pidx]);
    lua_call(L, length(pArray), LUA_MULRET); // Call the function
    end;
    finally
    luaL_unref(L, LUA_REGISTRYINDEX, funcRef); // Release the reference to the function
    end;
    end;
    lua_CallFunc := 0;
    end;[/pascal]

    The Lua "Prototype" for this would be:
    function CallFunc(NumTimes, FuncPointer, ...)

    Of course, for speed, when using a known function you should cache the function registry reference for re-use. Then release it when you no longer need it.

    Hope that helps some, just typed it in looking at some working code. But, there may be problems

  4. #34

    Getting started with LUA/pas2lua

    Uh, no, check the previous page. I cannot even register the functions... :?

    Quote Originally Posted by Robert Kosek
    Okay, this is silly. I found that TLua doesn't contain "RegisterMethod" so I had to use "LuaRegister" instead, but now, after changing a few things for compatibility I now get this when running the executable file:
    Code:
    C:\FPC\projects\luaexec>luaexec
    An unhandled exception occurred at $1000C33A :
    EAccessViolation : Access violation
      $1000C33A
      $00407067
      $004070CE
      $004013D2
    I updated the Lazarus project though I am not using that directly, just using straight FPC instead. It might be my register function for the CRT "library" but I really don't know. You can get the whole project from here. What'd I do this time? :?

    Also, I noticed numerous errors in my functions and the IFDEF clause the newer definition in the source is much better.

    EDIT:

    This is screwy. It's telling me that it doesn't want a pointer/address to the function, then I remove the @ symbol it immediately complains of too few arguments. What's going on!? This doesn't even raise an error in the LuaCRT unit, but in the main one it does. :?
    Also, I noticed that registering the CRT library functions caused that access violation; I don't know how, why, or how to fix it though.

  5. #35

    Getting started with LUA/pas2lua

    Okay, I tried this, and it raises the error that it has insufficient arguments.

    [pascal]procedure RegisterCRT(L: PLua_State);
    var
    idx : Integer;
    begin
    lua_pushliteral(L, 'crt');
    lua_newtable(L);
    idx := lua_gettop(L);
    LuaRegisterCustom(L, 'KeyPressed', lua_KeyPressed, idx);
    LuaRegisterCustom(L, 'ReadKey', lua_ReadKey, idx);
    LuaRegisterCustom(L, 'GetXY', lua_GetXY, idx);
    LuaRegisterCustom(L, 'TextColor', lua_TextColor, idx);
    LuaRegisterCustom(L, 'BackgroundColor', lua_BackgroundColor, idx);
    LuaRegisterCustom(L, 'ClrScr', lua_ClrScr, idx);
    LuaRegisterCustom(L, 'ClrEol', lua_ClrEol, idx);
    LuaRegisterCustom(L, 'DelLine', lua_DelLine, idx);
    LuaRegisterCustom(L, 'GotoXY', lua_GotoXY, idx);
    LuaRegisterCustom(L, 'InsLine', lua_InsLine, idx);
    LuaRegisterCustom(L, 'Window', lua_Window, idx);
    // lua_settable(L,idx);
    lua_settable(L, LUA_GLOBALSINDEX);
    end;[/pascal]

    But when I try this, it completely freaks out with the following error messages:[pascal]procedure RegisterCRT(L: PLua_State);
    var
    idx : Integer;
    begin
    lua_pushliteral(L, 'crt');
    lua_newtable(L);
    idx := lua_gettop(L);
    LuaRegisterCustom(L, 'KeyPressed', @lua_KeyPressed, idx);
    LuaRegisterCustom(L, 'ReadKey', @lua_ReadKey, idx);
    LuaRegisterCustom(L, 'GetXY', @lua_GetXY, idx);
    LuaRegisterCustom(L, 'TextColor', @lua_TextColor, idx);
    LuaRegisterCustom(L, 'BackgroundColor', @lua_BackgroundColor, idx);
    LuaRegisterCustom(L, 'ClrScr', @lua_ClrScr, idx);
    LuaRegisterCustom(L, 'ClrEol', @lua_ClrEol, idx);
    LuaRegisterCustom(L, 'DelLine', @lua_DelLine, idx);
    LuaRegisterCustom(L, 'GotoXY', @lua_GotoXY, idx);
    LuaRegisterCustom(L, 'InsLine', @lua_InsLine, idx);
    LuaRegisterCustom(L, 'Window', @lua_Window, idx);
    // lua_settable(L,idx);
    lua_settable(L, LUA_GLOBALSINDEX);
    end;[/pascal]
    Code:
    LuaCRT.pas&#40;76,68&#41; Error&#58; Incompatible type for arg no. 4&#58; Got "SmallInt", expected "<procedure>"
    LuaCRT.pas&#40;77,68&#41; Error&#58; Incompatible type for arg no. 4&#58; Got "SmallInt", expected "<procedure>"
    LuaCRT.pas&#40;78,68&#41; Error&#58; Incompatible type for arg no. 4&#58; Got "SmallInt", expected "<procedure>"
    LuaCRT.pas&#40;79,68&#41; Error&#58; Incompatible type for arg no. 4&#58; Got "SmallInt", expected "<procedure>"
    LuaCRT.pas&#40;80,68&#41; Error&#58; Incompatible type for arg no. 4&#58; Got "SmallInt", expected "<procedure>"
    LuaCRT.pas&#40;81,68&#41; Error&#58; Incompatible type for arg no. 4&#58; Got "SmallInt", expected "<procedure>"
    LuaCRT.pas&#40;82,68&#41; Error&#58; Incompatible type for arg no. 4&#58; Got "SmallInt", expected "<procedure>"
    LuaCRT.pas&#40;83,68&#41; Error&#58; Incompatible type for arg no. 4&#58; Got "SmallInt", expected "<procedure>"
    LuaCRT.pas&#40;84,68&#41; Error&#58; Incompatible type for arg no. 4&#58; Got "SmallInt", expected "<procedure>"
    LuaCRT.pas&#40;85,68&#41; Error&#58; Incompatible type for arg no. 4&#58; Got "SmallInt", expected "<procedure>"
    LuaCRT.pas&#40;86,68&#41; Error&#58; Incompatible type for arg no. 4&#58; Got "SmallInt", expected "<procedure>"
    LuaCRT.pas&#40;233&#41; Fatal&#58; There were 11 errors compiling module, stopping
    LuaCRT.pas&#40;5,50&#41; Fatal&#58; Compilation aborted
    Should I try "function as lua_CFunction" or "lua_CFunction(function)"? This is a bit crazy. I hope this doesn't occur very frequently when messing with Lua initially. :?

  6. #36

    Getting started with LUA/pas2lua

    What is the declaration of LuaRegisterCustom()?

  7. #37

    Getting started with LUA/pas2lua

    Lua.pas
    Code:
    type
      lua_CFunction = function&#40;L &#58; Plua_State&#41; &#58; Integer; cdecl;
    LuaRegisterCustom, LuaUtils.pas
    Code:
    procedure LuaRegisterCustom&#40;L&#58; PLua_State; TableIndex&#58; Integer; const Name&#58; PChar; F&#58; lua_CFunction&#41;;
    var
      Count&#58; Integer;
      S&#58; string;
    begin
      LuaProcessTableName&#40;L, Name, S, TableIndex, Count&#41;;
      LuaRawSetTableFunction&#40;L, TableIndex, S, F&#41;;
      lua_pop&#40;L, Count&#41;;
    end;
    All my functions are adhering to both call/definition correctly too. :? That's the strange part. I call the register function for the CRT functions I made, commenting out the search routine (I still cannot register it), and the instant they are registered (output isn't even started) that exception occurs and the application dies.

  8. #38

    Getting started with LUA/pas2lua

    Robert, you don't want to use the register methods from LuaUtils. Instead look at the register methods in my LuaWrapper and LuaObjects units.

    Post up a zip of your source files and I'll adjust it to work. Then you can compare the two and see what I'm talking about.

  9. #39

    Getting started with LUA/pas2lua

    Quote Originally Posted by Robert Kosek
    I updated the Lazarus project though I am not using that directly, just using straight FPC instead. It might be my register function for the CRT "library" but I really don't know. You can get the whole project from here. What'd I do this time? :?

    Also, I noticed numerous errors in my functions and the IFDEF clause the newer definition in the source is much better.

    EDIT:

    This is screwy. It's telling me that it doesn't want a pointer/address to the function, then I remove the @ symbol it immediately complains of too few arguments. What's going on!? This doesn't even raise an error in the LuaCRT unit, but in the main one it does. :?
    Is there something wrong with those register methods? They were the only really simple way to register the functions to a table that I could find. The way you gave me before FPC couldn't find the needed function. :?

    EDIT: You mean to try the RegisterMethod from LuaObject.pas? So, I should just use that instead I guess. Though, I honestly don't understand the duplicity and frequency of so many functions to try and do that same thing.

    EDIT2: As in something like this to register my functions?[pascal]procedure RegisterCRT(L: PLua_State);
    var
    idx : Integer;
    begin
    lua_pushliteral(L, 'crt');
    lua_newtable(L);
    idx := lua_gettop(L);
    RegisterMethod(L, 'KeyPressed', @lua_KeyPressed, idx);
    RegisterMethod(L, 'ReadKey', @lua_ReadKey, idx);
    RegisterMethod(L, 'GetXY', @lua_GetXY, idx);
    RegisterMethod(L, 'TextColor', @lua_TextColor, idx);
    RegisterMethod(L, 'BackgroundColor', @lua_BackgroundColor, idx);
    RegisterMethod(L, 'ClrScr', @lua_ClrScr, idx);
    RegisterMethod(L, 'ClrEol', @lua_ClrEol, idx);
    RegisterMethod(L, 'DelLine', @lua_DelLine, idx);
    RegisterMethod(L, 'GotoXY', @lua_GotoXY, idx);
    RegisterMethod(L, 'InsLine', @lua_InsLine, idx);
    RegisterMethod(L, 'Window', @lua_Window, idx);
    // lua_settable(L,idx);
    lua_settable(L, LUA_GLOBALSINDEX);
    end;[/pascal]Attempting that immediately gave me this when attempting to compile:
    Code:
    LuaObject.pas&#40;462,49&#41; Error&#58; Incompatible type for arg no. 3&#58; Got "<address>", expected "<procedure>"
    LuaObject.pas&#40;463,55&#41; Error&#58; Incompatible type for arg no. 3&#58; Got "<address>", expected "<procedure>"
    LuaObject.pas&#40;464,46&#41; Error&#58; Incompatible type for arg no. 3&#58; Got "<address>", expected "<procedure>"
    LuaObject.pas&#40;465,43&#41; Error&#58; Incompatible type for arg no. 3&#58; Got "<address>", expected "<procedure>"
    LuaObject.pas&#40;466,46&#41; Error&#58; Incompatible type for arg no. 3&#58; Got "<address>", expected "<procedure>"
    LuaObject.pas&#40;467,43&#41; Error&#58; Incompatible type for arg no. 3&#58; Got "<address>", expected "<procedure>"
    LuaObject.pas&#40;484,49&#41; Error&#58; Incompatible type for arg no. 3&#58; Got "<address>", expected "<procedure>"
    LuaObject.pas&#40;485,55&#41; Error&#58; Incompatible type for arg no. 3&#58; Got "<address>", expected "<procedure>"
    LuaObject.pas&#40;487,43&#41; Error&#58; Incompatible type for arg no. 3&#58; Got "<address>", expected "<procedure>"
    LuaObject.pas&#40;488,46&#41; Error&#58; Incompatible type for arg no. 3&#58; Got "<address>", expected "<procedure>"
    LuaObject.pas&#40;516&#41; Fatal&#58; There were 10 errors compiling module, stopping
    LuaObject.pas&#40;4,50&#41; Fatal&#58; Compilation aborted
    Ick; at the moment I'm reconsidering trying to learn Lua using FPC. FPC just isn't very friendly to me no matter what I do, whenever I try.

  10. #40

    Getting started with LUA/pas2lua

    I just noticed something, but Jeremy, why are you pushing a procedure like a pointer, when you aren't using the right type? If you adjust your types to include a lua_PCFunction (in Lua.pas) like so:[pascal]type
    lua_CFunction = function(L : Plua_State) : Integer; cdecl;
    lua_PCFunction = ^lua_CFunction;[/pascal]And then change your RegisterMethod and lua_pushcfunction to use the lua_PCFunction type it seems to work. You're treating the function like a pointer and FPC is choking all over itself on this. Either it says "I don't want an address!" or when you pass a function "where are the variables for the function?" and so it doesn't work either way in its present implementation.

Page 4 of 5 FirstFirst ... 2345 LastLast

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
  •