PDA

View Full Version : Stuck with Lua file locations



jon1977
January 28th, 2009, 12:31 PM
Hi all,
I'm still 'tinkering' with Girder, trying to learn all the basics and building up lots if snippits of code for the building blocks of what I'm after.
I'm currently stuck on where the best place is for Lua files. Some are obvious i.e. Transport for transport items, startup for files which run on startup, but there are loads more default locations and I'm struggling for the following two groups:

1) A Lua file which could have a simple set of actions, could be used to poll serial data, get the result and then carry out a mathmatical function and then put the answer into a variable. I'd want this to continiously run (my app is to read a room temperature, compare it to a setpoint and set a boolean flag to true or false it is is above or below the setpoint)' I'd want it to continiously do this. It works in the startup folder, but I'm intreaged whether this is the best place (ensuring that it can not seriously load up the processor). I've read up on threads to avoid this but not got as far as getting it working yet.

and....

2) A Lua file which is 'called' or 'run' from another Lua file. Not sure how best to explain this, but lets say for argument I had a very simple Lua file which just printed "Hello World" in the interactive Lua console,
if I called it : helloworld.lua
and it contained just

print ("Hello World")

and I wanted to run that file based on an event (such as press a remote control button), and I know I could create a script action in the treescript, but where would the file go and how to I load / run / activate it? I'm guessing that I have to declare a function and then call it?? Really stuck on this one!

On a side note, if it will help others, I'll stick my notes in a post on the Lua code snippets forum. I'm fairly on the ball with the basis behind various forms of code, but with subtle differences between them its often the small things that catch people out and could put lots of people off. Although the manuals are fairly comprehensive, they do make some assumptions which require a lot of trawling through other pieces of code to de scrample. Not a moan or bad point by any means, just an observation from someone trying to learn!

Any help on the Lua locations and how to call or load them would be appreciated.

Cheers
Jon

harleydude
January 28th, 2009, 12:53 PM
2) A Lua file which is 'called' or 'run' from another Lua file. Not sure how best to explain this, but lets say for argument I had a very simple Lua file which just printed "Hello World" in the interactive Lua console,
if I called it : helloworld.lua
and it contained just

print ("Hello World")

and I wanted to run that file based on an event (such as press a remote control button), and I know I could create a script action in the treescript, but where would the file go and how to I load / run / activate it? I'm guessing that I have to declare a function and then call it?? Really stuck on this one!

I have created another folder in the Girder5\luascript folder call MyScripts. I put those types of scripts there. When I need to call it from another script I include require ('MyScripts.ScriptFilename'), then call the method contained in the script.

Sample Lua File saved as MyScript.lua:

MyScript = MyScript or {} -- establishes a variable space to contain the methods, this keeps all available methods in the MyScript table

function MyScript.HelloWorld() -- prefix each method with MyScript to include this method in the MyScript variable space
print ('Hello World')
end
function MyScript.GoodbyeWorld() -- prefix each method with MyScript to include this method in the MyScript variable space
print ('Goodbye World')
end

Action Script Code:

require ('MyScripts.MyScript')
MyScript.HelloWorld()
MyScript.GoodbyeWorld()

jon1977
January 29th, 2009, 01:58 AM
Excellent, just what I'm after thanks.
As I said, logical and obvious when you know how!
Cheers
Jon.