Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: Scripts in the \lua directory

  1. #1
    Join Date
    May 2004
    Location
    Cardigan, UK
    Posts
    9,278

    Default Scripts in the \lua directory

    I've only just realised that NR autoloads any scripts in the \lua directory.

    What happens if you have a function OnVariableChange() in one of those scripts and also in your CCF's lua file? Do they both get called, or does the one in the CCF's lua file replace the one in the autoloaded script?

    I think I would expect that it would be the second of these, but it would be nice if it were the first.
    --Rob

  2. #2
    Join Date
    Jan 2004
    Posts
    1,859

    Default

    You could try it and see :wink:
    JLee
    OpusNR & JLee CCF/GML available in the downloads section.

  3. #3
    Join Date
    May 2002
    Location
    CT
    Posts
    6,559

    Default

    I believe whatever is loaded last will be the "correct one".

    Something in my queue is to have proper callbacks, by registering, so you can have more than one "OnVariableChange", for instance.

  4. #4
    Join Date
    May 2004
    Location
    Cardigan, UK
    Posts
    9,278

    Default

    That sounds good.

    Could probably do that with Lua come to think of it. You could even register interest in a variable by name.

    How does the autoload mechanism work? Is it the equivalent of calling 'require' for each script in the \lua directory. ie is it safe to call require() for any lua file in that directory?
    --Rob

  5. #5
    Join Date
    May 2002
    Location
    CT
    Posts
    6,559

    Default

    I'm not sure if it's the equivalent. Basically any file found is parsed and run, so I guess it's more like "dofile".

    The standard SDK variable callback requires registration of a variable name, but I made lua call one generic function. It theoretically would be easy enough to "hook" specific variables with callbacks in the tables in lua.

  6. #6
    Join Date
    May 2004
    Location
    Cardigan, UK
    Posts
    9,278

    Default

    Here's something I knocked up last night - seems to work

    Code:
    local Monitors = {}
    
    function RegisterVariableWatch(varName, func) -- func should take two parameters varName, and varValue
        Monitors[varName] = Monitors[varName] or {}
        local temp = Monitors[varName]
        temp[table.getn(temp)+1] = func
    end
    
    function OnVariableChange(varName, varValue)
        local monList = Monitors[varName]
        if monList then
            for _, func in ipairs(monList) do
                func(varName, varValue)
            end
        end
    end
    You just save this code as a .lua file in NRs \lua directory.

    Obviously you need to delete or rename your OnVariableChange function or it will override this one. Here's an example from my CCF's lua file :-
    Code:
    function WatchFilter(varName, varValue)
       NetRemote.ExecuteAction(-1, 0, 1, "LDJ.SELECTNAMEDFILTER("..varValue..")")
    end
    
    function OnCCFLoad()
         RegisterVariableWatch('LDJ_Base_Filter_Type', WatchFilter)
    end
    Note that you can have more than one function monitoring the same variable. They will be called in the order that they are registered.

    I think this looks cleaner than multiple conditions in the OnVariableChange function and might well be faster.

    It wouldn't be hard to do something similar for OnPanelLoad
    --Rob

  7. #7
    Join Date
    Jul 2004
    Location
    London UK
    Posts
    1,249

    Default

    Rob,

    This is neat.

    But I would still think it better for Ben to add a built-in registration mechanism to NR. This registration already exists for drivers, who register an interest in specific variables. AvidUtils uses that *a lot*. I would expect (although I don't actually know) that there could be a big performance gain in *not* calling into LUA whenever *any* NR variable changes its value.

    Brian

  8. #8
    Join Date
    May 2004
    Location
    Cardigan, UK
    Posts
    9,278

    Default

    Oh, I couldn't agree more, this is only a stop gap until Ben adds equivalent functionality.
    --Rob

  9. #9
    Join Date
    May 2002
    Location
    CT
    Posts
    6,559

    Default

    Until I add "proper" support, Rob, can I add this .lua script into the lua directory in a default install?

    That would allow someone in a new lua script to just call your method.

    I suppose I should do something similar for OnPanelLoad (and possibly OnCCFLoad) as well.

  10. #10
    Join Date
    May 2004
    Location
    Cardigan, UK
    Posts
    9,278

    Default

    Feel free.

    I'm using it in the latest version of LDJ - as a file called VariableWatch.lua
    --Rob

Posting Permissions

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