supplierdeeply

Lua Scripting Integration

  • 46 Replies
  • 15486 Views
*

Offline Jucifer

  • *****
  • 251
  • [A710IS]
  • Publish
    Re: Lua Scripting Integration
    « Reply #20 on: 11 / May / 2008, 06:34:00 »
    Advertisements
    Yes, pretty much the math.random.
    Would adding something like

    static int luaCB_random( lua_State* L )
    {
      math.random( L, shooting_get_prop( luaL_checknumber( L, 1 ), luaL_checknumber( L, 2 ) ) );
      return 1;
    }

    to luascript.c suffice?
    And what would be the correct lines to add? :]

    Also, could a function for executing camera commands directly (exec(camera_function_to_execute(parameters, for, the, function))) be easily added?

    *

    Offline Velo

    • *
    • 30
  • Publish
    Re: Lua Scripting Integration
    « Reply #21 on: 11 / May / 2008, 06:51:11 »
    Hi Jucifer,

    math.random will be defined in lmathlib.c. No need to define it in luascript.c
    Just now I finished the mentioned math functions. Still untested, but I don't expect any problems.
    Before publishing a new patch I want to include 'require' support to load arbitrary Lua Sub-Scripts.
    This will really rock.


    *

    Offline Velo

    • *
    • 30
  • Publish
    Re: Lua Scripting Integration
    « Reply #22 on: 11 / May / 2008, 07:04:16 »
    Also, could a function for executing camera commands directly (exec(camera_function_to_execute(parameters, for, the, function))) be easily added?

    camera_function_to_execute must be bound to lua (luascript.c).

    But the Lua philosophy is to bind only the most basic function to Lua and define new convenience functions within Lua itself.

    Like binding get_prop and defining a get_mode function.
    Something like this (for my IXUS 70/untested)

    Code: [Select]
    function get_mode()
      local pg = getprop(49)
      if pg == -32764 then
        return "manual"
      elseif pg == -32768 then
        return "auto"
      elseif
      ...
    end

    This functions can be defined in a submodule like chdk.lua and all scripts can use this functions after calling require:

    Code: [Select]
    require "chdk"

    if get_mode() == "manual" then
      print "good choice"
    end

    Well, this will only work with the next patch.


    *

    Offline Jucifer

    • *****
    • 251
    • [A710IS]
  • Publish
    Re: Lua Scripting Integration
    « Reply #23 on: 11 / May / 2008, 07:59:46 »
    math.random will be defined in lmathlib.c.
    I noticed that, when searching if random function existed already...

    Quote
    include 'require' support to load arbitrary Lua Sub-Scripts.
    This will really rock.
    Sounds really good. Rock on! :]

    If there was a camera function, that would call another function with the parameters supplied, and then the former is bound in luascript.c..? Or ...something. :D (I might like the ability to call any camera function from a Lua script. Would it make any sense?)

    Lua to the trunk!


    *

    Offline Velo

    • *
    • 30
  • Publish
    Re: Lua Scripting Integration
    « Reply #24 on: 11 / May / 2008, 08:36:02 »
    (I might like the ability to call any camera function from a Lua script. Would it make any sense?)

    I think you are looking for something like C/Invoke. Where you can call any C function from Lua without binding it.
    But this will not work as-is on the cam. You don't have something like dlsym to find a function by its name. So you still have to register all callable function somewhere.

    Lua to the trunk!

    You have my vote. :)


    *

    Offline Velo

    • *
    • 30
  • Publish
    Re: Lua Scripting Integration
    « Reply #25 on: 11 / May / 2008, 08:59:56 »
    Patch#4 is ready for take off:

    zSHARE - chdk-lua-4.patch.zip

    New features:
    - New math functions (abs,min,max,pow,random,randomseed)
      They are in table 'math'. Must be used like so: math.max( 2, 3 )

    - require function (and dofile/loadfile)
      require loads a file out of the scripts folder. This means one can now create a library of functions and recycle this lib in scripts with a call to require "libname".
      (libname is the filename without '.lua' suffix)


    *

    Offline fudgey

    • *****
    • 1705
    • a570is
  • Publish
    Re: Lua Scripting Integration
    « Reply #26 on: 11 / May / 2008, 11:47:12 »
    - require function (and dofile/loadfile)
      require loads a file out of the scripts folder. This means one can now create a library of functions and recycle this lib in scripts with a call to require "libname".
      (libname is the filename without '.lua' suffix)

    Should we consider using some other directory than chdk/script for loading libraries (such as chdk/lib chdk/scripts/lib or chdk/lua/lib or or chdk/scripts/lua/lib) or giving the user an option to change the lua library load path?

    The "load script" file browser gets crowded easily, I already have subdirectories for different types of scripts and only have a few scripts in the actual chdk/scrips directory. Forcing all libraries (which may not even work as standalone scripts) to exist in that directory clutters the script load browser making it more cumbersome to select scripts.

    (I haven't had the time to test the lua builds, but for what I've read it sounds awesome and needs to find it's way into trunk someday soon if you think there won't be any large modifications which would likely break compatibility with old and new lua scripts).

    *

    Offline Jucifer

    • *****
    • 251
    • [A710IS]
  • Publish
    Re: Lua Scripting Integration
    « Reply #27 on: 11 / May / 2008, 12:28:03 »
    You can put libname.lua to chdk/script/libs and
    require "libs/libname"
    .
    Would chdk/lib or chdk/lualib be ok? Less scrolling, when selecting scripts. :]


    *

    Offline Velo

    • *
    • 30
  • Publish
    Re: Lua Scripting Integration
    « Reply #28 on: 11 / May / 2008, 12:57:31 »
    Would chdk/lib or chdk/lualib be ok? Less scrolling, when selecting scripts. :]

    You mean lib not within scripts folder? Or do you mean scripts/lib?

    I would prefer chdk/scripts/lib

    Normally require would honor the package.path variable to find the script. But this funtionality requires some string manipulation functions I have not compiled in.
    Instead I hardcoded the path A/CHDK/SCRIPTS/?.lua where ? is the argument to require.

    This means that scripts from subfolders will be found with require "folder/script" as Jucifer pointed out. And I find it not too bad.

    *

    Offline Jucifer

    • *****
    • 251
    • [A710IS]
  • Publish
    Re: Lua Scripting Integration
    « Reply #29 on: 11 / May / 2008, 13:10:27 »
    I was thinking of not putting anything but scripts to the chdk/scripts folder. If (when) the path is hardcoded to chdk/scripts, would it be possible to load libs from e.g. chdk/lib? I think I tried
    require "../lib/libname"
    but no avail.

     

    Related Topics