Basic Lua question

I am trying to test a shared lua module in the Test Luup Code window. I am using require to get access to the shared functions. Whenever I attempt to access functions in the shared module, I get a Code Failed message. To test this problem, I have done the following:

Place the file TestMod.lua in the /etc/cmh-ludl directory on Vera3.

TestMod.lua contains:

function testfunc(testparm) return testparm end

In the Test Luup Code window, I enter :

tst = require("/etc/cmh-ludl/TestMod") tst.testfunc("parm")

Running this produces the Code Failed message on clicking GO

What am I getting wrong?

Thanks,

I believe you need to upload the file via Vera’s web interface Apps → Developer Apps → Lua Files. The files are compress when uploaded. So this may be your problem.

  • Garrett

I am not privy to the inner workings of Vera, so I cannot tell for sure.

One possible cause is that that Vera has several “run stacks” operating, and functions visible in one stack are not visible in another.

For example, on my Vera 1, the “Test LUA” code window is a different run time stack from the normal scenes’ stack. So variables and functions declared in the Test Lua window will not be visible to my scene LUA code.

I do not know if this is the same in later versions of Vera.

You are correct … that’s so that one device does not corrupt another.
Each device (and the test LUP code) run’s in their own sandbox.

You need to require(“Module”) in each sandbox.
This will give you access to the functions, but the variables will be unique for each sandbox.

To share data between Sandboxes you need to use Device State Variables.

Welcome to land of Vera Developers!

/etc/cmh-ludl/TestMod.lua:

[code]module(…,package.seeall)

function testfunc(testparm)
return testparm
end[/code]

Sandbox:

tst = require("TestMod") tst.testfunc("parm")

([font=courier]/etc/cmh-ludl[/font] is in the search path, so you could leave it out.)

Thanks guys.

The code ran ok without compression to lzo. Maybe this is because it is outside normal Luup processing.

The module(…,package.seeall) statement did the trick, not that I understand exactly what this is doing just yet.

I had figured that I would need a kind of singleton device if I need any global variables.

http://lua-users.org/wiki/ModulesTutorial

Thanks oTi@