Cool VL Viewer forum
http://sldev.free.fr/forum/

Lua scripting related requests
http://sldev.free.fr/forum/viewtopic.php?f=9&t=1737
Page 3 of 10

Author:  Henri Beauchamp [ 2017-02-24 21:49:58 ]
Post subject:  Re: Lua scripting related requests

ZaneZimer wrote:
I have a question not directly related to your additions to the viewer for executing Lua, but rather using Lua itself. I currently have a single .lua file and am using luac to compile it to bytecode for the viewer to pick up. All that works great, but my automation.lua is getting large especially since I have added serial/deserialize methods to make use of the new user data settings.

From what I have read, I should be able to create multiple .lua files and using luac combine them into a single bytecode file. A single file is created but I cannot seem to call any functions defined locally in the other files. I realize I can't create my own modules because of the 'require' restriction but I'm trying to achieve some form of compartmentalization of my own code since I will always compile. Maybe I just don't understand the usage of 'local' as it pertains to a separate file once it is compiled into bytecode. It seems to act more like it's private. I had been trying to avoid using globals in my automation.lua, maybe they are OK in the auxiliary files? Total noob at Lua. :?
Even loadfile() (and dofile(), which I forgot to disable, and will get disabled in tomorrow's release), you'd just execute the file as another chunk (i.e. in a "child" Lua state, with its own locality), so you won't be able to reach functions defined in that loaded file...

To achieve what you want, you may simply split your program into several *.lua files, then specify the said files (in the proper order, so that the main program that will call functions defined in other files, will be specified last) to luac:
Code:
luac5.3 -o automation.luac my_lua_functions.lua automation.lua


You could also use a pre-processor (like "cpp" in C/C++)... AFAIK, there is no official pre-processor for luac, but (under Linux, or with mingw/cygwin under Windows) you could just use cpp itself, using #include "some_included_lua_file.lua" in your main automation.lua "sources".

Example for automation.lua:
Code:
#include "my_lua_functions.lua"

hello_world()

Example for my_lua_functions.lua:
Code:
function hello_world()
    print("Hello world !")
end

Then, using cpp to assemble your Lua script and pass it to luac:
Code:
cpp -P -nostdinc -pipe automation.lua | luac5.3 -o automation.luac -

The "-P" option instructs cpp not to add its own comments/references in the output file (since luac won't understand them), "-nostdinc" tells it not to search for C headers outside the current directory (where both your "my_lua_functions.lua" and "automation.lua" should reside: if "my_lua_functions.lua" is elsewhere you can tell cpp with its "-I" option where to look for), and "-pipe" allows to send the resulting pre-processed sources to the standard output, which gets connected to the standard input of luac thanks to the pipe ("|") character and the "-" last option in luac's command line.

Now, check that it worked:
Code:
lua5.3 automation.luac
Which should print: Hello world !

As a bonus, you could use most cpp directives (#define, #if/#else/#endif, etc) in your code...

Author:  ZaneZimer [ 2017-02-25 00:25:03 ]
Post subject:  Re: Lua scripting related requests

Great info, Henri. Thank you. The cpp trick was something I hadn't considered. I was trying for an all Lua solution.

I did managed to get something interesting going as follows:
Code:
<module.lua>
local util = function()
  print("I'm a utility")
end

Module = {
  util = util
}

return Module
<end module.lua>

<main.lua>
local function main(args)
  Module.util()
end

main()
<end main.lua>
As long as module.lua is loaded first, i.e. passed to luac before main.lua, things work pretty much like a regular module. The only drawback is that Module must be global. This seems to be similar to the syntax for a 'real' module that would need 'require' and could use a local Module declaration.

Author:  ZaneZimer [ 2017-02-28 14:47:12 ]
Post subject:  Re: Lua scripting related requests

What is the maximum size limit of the global/per account data items? I am currently serializing a relatively small 'config' table into the per account data but I do not wish to overrun the limits.

Any thoughts on using notecards for holding and reading in larger bits of data? Maybe in a read only capacity?

Cheers

Author:  Henri Beauchamp [ 2017-03-01 00:10:23 ]
Post subject:  Re: Lua scripting related requests

ZaneZimer wrote:
What is the maximum size limit of the global/per account data items? I am currently serializing a relatively small 'config' table into the per account data but I do not wish to overrun the limits.

That's the limit of a string in a LLSD representation: practically "unlimited" (I just made a try with a 5+ millions characters string with SetGlobalData()/GetGlobalData() and it did work)...

Quote:
Any thoughts on using notecards for holding and reading in larger bits of data? Maybe in a read only capacity?
Notecards are way more limited (64K, IIRC) and they cannot be read on viewer start (only after a successful login), are slow to load, and may prove unreliable (asset server "hiccups"...). They would also not be suitable for anything else than per-account settings... Definitely a no-no.

Author:  ZaneZimer [ 2017-03-01 00:19:58 ]
Post subject:  Re: Lua scripting related requests

Henri Beauchamp wrote:
That's the limit of a string in a LLSD representation: practically "unlimited" (I just made a try with a 5+ millions characters string with SetGlobalData()/GetGlobalData() and it did work)...
Ah, quite large then. That is more than enough for what I'm doing.
Henri Beauchamp wrote:
Notecards are way more limited (64K, IIRC) and they cannot be read on viewer start (only after a successful login), are slow to load, and may prove unreliable (asset server "hiccups"...). They would also not be suitable for anything else than per-account settings... Definitely a no-no.
That's fine. I was only pondering things that a user could edit that would drive script behavior and be 'file system safe'. Since the data values can store large strings, I am content to use them.

Author:  ZaneZimer [ 2017-03-18 20:00:37 ]
Post subject:  Re: Lua scripting related requests

Should the OnWindlightChange() event fire when 'Revert to region default' is selected? or any of the 'time of day' selections?

Author:  Henri Beauchamp [ 2017-03-23 16:38:08 ]
Post subject:  Re: Lua scripting related requests

ZaneZimer wrote:
Should the OnWindlightChange() event fire when 'Revert to region default' is selected? or any of the 'time of day' selections?

Both OnWindlightChange() and ApplySkySettings() will be extended to take those into account.

Author:  ZaneZimer [ 2017-03-25 19:50:59 ]
Post subject:  Re: Lua scripting related requests

Quote:
Both OnWindlightChange() and ApplySkySettings() will be extended to take those into account.
Thanks Henri. Both work nicely. I do notice that the Use Estate Time in the Environment Editor must not trigger the same action as Revert to region default, because the OnWindlightChange() doesn't seem to fire.

Author:  Henri Beauchamp [ 2017-03-25 23:10:41 ]
Post subject:  Re: Lua scripting related requests

ZaneZimer wrote:
I do notice that the Use Estate Time in the Environment Editor must not trigger the same action as Revert to region default, because the OnWindlightChange() doesn't seem to fire.
Added for next release. Let me know if there are other places, in the viewer, where such changes can be triggered (I'm not too familiar with Windlight stuff, since I never use it myself)...

Author:  ZaneZimer [ 2017-03-25 23:35:34 ]
Post subject:  Re: Lua scripting related requests

Quote:
I'm not too familiar with Windlight stuff, since I never use it myself
No worries. I don't/didn't much either. I have just been tinkering around with the Lua scripting and found this during testing.

Page 3 of 10 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/