Questions & best practice
Hi Brett,
I've been wondering about the scripts and have a couple of questions for you, hope you don't mind, I've just chucked em into a list:
1. Why lots of scripts (per function), is this necessary?
I suspect this is down to efficiency, having observed the plugboard, it only has to run a small script if an event is triggered....am I close? How many function would you suggest to run in one script?
2. Is it possible to pass variables accross scripts
i.e. set a global variable of presence=true -- someone is at home
or dusk=true -- it's dark out
and then be able to work with these variables from any script?
3. Why use "local" in front of a variable, does that just limit the variable to that function or that script file?
4. Do I need to be careful about variable names accross my scripts, so as not to have same variables names?
5. What is a class used for as opposed to a target or a source?
i.e can you explain laymans terms why I would register a class as opposed to a source? as in your alias examples.
The whole LUA thing is just alien to me and I think the answers to some of the questions above, may just help me step forward a little.
Cheers
Gary
1. As Derek said its more a management feature.. There is no reason why you need a script (per function). However if you want to share snippet of code mosty you'd share just a "BIT" which happens to be at the FILE level.
2. Yes via the CACHE only. If you want to share variables then you have to place BOTH functions in the same script.
3. LOCAL is a scope thing. It limits variable scope in the SCRIPT to JUST where its used otherwise the variable is availably GLOBALLY inside that script.
4. Each SCRIPT file runs in its own interpreter which is why 2. isn't possible. In terms of memory efficiency each FILE incurs some additional memory overhead to establish this interpretter context but its minimal. It also means if a SCRIPT crashes it won't take out the other file based scripts!
5. Class is simply there for orthoganality. If it wasn't there somebody would find a need for it. Mosty TARGET/SOURCE would be used.
Brett
I was new to Lua scripting before Brett rolled it into the HAH. It's a fine scripting language. I can recommend this book http://www.amazon.co.uk/Programming-Lua-Second-Roberto-Ierusalimschy/dp/...
Of course, the coolest piece about the Plugboard is the way that Brett has implemented xAP message caching and access to the cache via Lua.
Re your Qs. ... (Brett will correct me if I'm not getting this right)
1. You could just lump all the code into one big script. However, I reckon that having things broken down by function makes for an easier life.
2. I don't think that there is a way to declare a Lua variable that can be seen across all scripts. However, you could send an xAP message containing the variable value and then use the cache access to find this in other scripts.
3. 'local' declares the variable as having a scope of just that function or 'chunk' of Lua code. See http://www.lua.org/pil/4.2.html
4. I don't think so. I reckon that each script runs in its own Lua interpreter 'environment'
5. 'Class' is an xAP protocol thing. Description is here http://www.xapautomation.org/index.php?title=Protocol_definition#Message...
In practice, I reckon that most plugboard scripts are going to be looking for specific sources and targets.