Page 3 of 5 FirstFirst 12345 LastLast
Results 21 to 30 of 42

Thread: Getting started with LUA/pas2lua

  1. #21

    Getting started with LUA/pas2lua

    That article does show one way of doing libraries in your code. Here is what I do for libraries though:[pascal]procedure RegisterBackgroundMusic(L : Plua_State);
    var
    idx, mt : Integer;
    begin
    lua_pushliteral(L, 'BackgroundMusic');
    lua_newtable(L);
    idx := lua_gettop(L);
    RegisterMethod(L, 'Play', @BackgroundMusic_Play, idx);
    RegisterMethod(L, 'PlaySong', @BackgroundMusic_PlaySong, idx);
    RegisterMethod(L, 'Stop', @BackgroundMusic_Stop, idx);
    RegisterMethod(L, 'AddMusicFile', @BackgroundMusic_AddMusicFile, idx);
    RegisterMethod(L, 'Delete', @BackgroundMusic_Delete, idx);
    RegisterMethod(L, 'Next', @BackgroundMusic_Next, idx);
    RegisterMethod(L, 'Previous', @BackgroundMusic_Previous, idx);
    RegisterMethod(L, 'First', @BackgroundMusic_First, idx);
    RegisterMethod(L, 'Last', @BackgroundMusic_Last, idx);
    lua_newtable(L);
    mt := lua_gettop(L);
    RegisterMethod(L, '__index', @index_BackgroundMusic, mt);
    //RegisterMethod(L, '__newindex', @index_BackgroundMusic, idx);
    RegisterMethod(L, '__newindex', @newindex_BackgroundMusic, mt);
    //RegisterMethod(L, '__mt', @newindex_BackgroundMusic, idx);
    lua_setmetatable(L, idx);
    lua_settable(L, LUA_GLOBALSINDEX);
    end;[/pascal]

    That should at least give you an idea of how it can work. The nice thing about Lua is there are literally 100's of ways to achieve a single task. The bad thing about Lua is there are literally 100's of ways to achieve a single task.

    Sorry, that code basically creates a table called BackgroundMusic (the same thing as a library in the end). So you call methods out of it as BackgroundMusic.MethodName(Params)

  2. #22

    Getting started with LUA/pas2lua

    Well, that'll help.

    I can't say that I paid too much attention to the details about your engine before, but I was surprised that you are making that for paying customers. Congratulations! That's one of the hardest things to do sometimes, make an engine especially for a paying customer. No wonder you keep it very near and dear to your heart, and I don't blame you.

    So, I assume the function looks right for returning no results? And just general structure too, of course.

  3. #23

    Getting started with LUA/pas2lua

    It looks fine to me, though you might want to look into lua_error instead of assert .

  4. #24

    Getting started with LUA/pas2lua

    Err, I cannot figure out how to actually use it. :? I don't have the reference manual at the moment, so what are the parameters and usage?

  5. #25

    Getting started with LUA/pas2lua

    C Prototype is: lua_error (lua_State *L, const char *s)

    so:[pascal]lua_error(L, 'My Error Message');[/pascal]

  6. #26

    Getting started with LUA/pas2lua

    Then how do I call it, given I find this thing, when in use that there's a pushstring occurring first if I keep searching.

    Code:
    function lua_error(L : Plua_State) : Integer;
      cdecl; external LuaDLL;
    Do I?:
    Code:
    lua_pushstring(L,'Error!');
    lua_error(L);
    EDIT:

    Found it, LuaUtils.pas. Sorry, should've checked there.

  7. #27

    Getting started with LUA/pas2lua

    I wrote this function for debugging purposes, along the lines of assert and error combined, and I'm finding it a great shortcut! You might find some use for it somewhere, or want to include it in the library.

    [pascal]{
    lua_AssertError(lua_state, condition, error message)
    }
    function lua_AssertError(L: PLua_State; Condition: Boolean; const Msg: string): boolean;
    begin
    if condition then
    LuaError(L,Msg);
    {$IFDEF FPC}
    lua_AssertError := condition;
    {$ELSEIF}
    result := condition;
    {$ENDIF}
    end;[/pascal]

    It's not much really, but it makes structuring things easy! Example:[pascal]function lua_Window(L: PLua_State): integer; cdecl;
    begin
    if not lua_AssertError(L, lua_gettop(L) <> 4, 'Window() requires four parameters, x1, y1, x2, y2.') then
    if not lua_AssertError(L,not (lua_isnumber(L,1) and lua_isnumber(L,2) and
    lua_isnumber(L,3) and lua_isnumber(L,4)),'All parameters of Window() must be numbers or numerical strings.') then begin
    // Being sneaky here, and cheating a little.
    Window(LuaToInteger(L,1),LuaToInteger(L,2),LuaToIn teger(L,3),LuaToInteger(L,4));
    end;
    lua_Window := 0;
    end;[/pascal]

    Just remember that it evaluates true on error, shows the message, and then returns true that there was an error. Just in case someone other than us finds use for it. But that simple little thing is saving me debugging time like crazy...

    In fact I turned this:[pascal]function lua_GotoXY(L : PLua_State) : integer; cdecl;
    var x,y: integer;
    begin
    lua_GotoXY := 0;


    if lua_gettop(L) = 2 then begin
    if not lua_AssertError
    if lua_isnumber(L,1) then
    x := lua_tointeger(L,1)
    else
    lua_AssertError(L,false, 'Argument 1 in GotoXY() is not a number.');

    if lua_isnumber(L,2) then
    y := lua_tointeger(L,2)
    else
    lua_AssertError(L,false, 'Argument 2 in GotoXY() is not a number.');

    lua_AssertError(L, (x = 0) and (y = 0), 'Coordinates must be greater than 0.');

    GotoXY(x,y);
    end else lua_AssertError(L,false, 'Error, GotoXY() requires two arguments of type integer.');
    end;[/pascal]
    Into this:[pascal]function lua_GotoXY(L : PLua_State) : integer; cdecl;
    var x,y: integer;
    begin
    lua_GotoXY := 0;

    if not lua_AssertError(L, lua_gettop(L) <> 2, 'Error, GotoXY() requires two arguments of type integer.') then
    if not lua_AssertError(L, not (lua_isnumber(L,1) and lua_isnumber(L,2)), 'GotoXY() requires two number friendly values.') then begin
    x := lua_tointeger(L,1)
    y := lua_tointeger(L,2)
    if not lua_AssertError(L, (x = 0) or (y = 0), 'Coordinates must be greater than 0.') then
    GotoXY(x,y);
    end;
    end;[/pascal]

    So much easier to understand.

  8. #28

    Getting started with LUA/pas2lua

    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&#58;\FPC\projects\luaexec>luaexec
    An unhandled exception occurred at $1000C33A &#58;
    EAccessViolation &#58; 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. :?

  9. #29

    Getting started with LUA/pas2lua

    Quote Originally Posted by Robert Kosek
    lua.pas(978,3) Error: Identifier not found "result"
    {$mode delphi} // fixes this
    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

  10. #30

    Getting started with LUA/pas2lua

    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.

Page 3 of 5 FirstFirst 12345 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
  •