We have moved at community.getvera.com

Author Topic: Can luup.devices be accessed from a call_timer callback?  (Read 1635 times)

Offline Richard

  • Sr. Newbie
  • *
  • Posts: 34
  • Karma: +0/-0
Can luup.devices be accessed from a call_timer callback?
« on: July 17, 2011, 01:51:21 pm »
I'm new to lua and plugin programming, so I don't know what scope various symbols have.

I have a function that gets called by call_timer that tries to access luup.devices.  It tries to do a lookup into it used a device number.  Since the same code works from the startup function, this leads me to believe that luup.devices is not in scope for my call_timer function.

Should I be able to access luup.devices from a function that is passed to call_timer()?

Does anyone have any good pointers to doc on how scope is handled in general in lua/luup?

Thank you

Offline futzle

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3260
  • Karma: +192/-9
Re: Can luup.devices be accessed from a call_timer callback?
« Reply #1 on: July 17, 2011, 05:32:28 pm »
I have a function that gets called by call_timer that tries to access luup.devices.  It tries to do a lookup into it used a device number.  Since the same code works from the startup function, this leads me to believe that luup.devices is not in scope for my call_timer function.
Should I be able to access luup.devices from a function that is passed to call_timer()?

Yes, you should.  luup.* is accessible everywhere, in every piece of Lua code that Vera runs.  So there'll be a different reason for your code not working in the context of a call_timer().

Quote
Does anyone have any good pointers to doc on how scope is handled in general in lua/luup?

The definitive reference is the Lua manual (http://www.lua.org/manual/5.1/manual.html).  Section 2.3 describes variables and scope:

Quote
Any variable is assumed to be global unless explicitly declared as a local (see §2.4.7). Local variables are lexically scoped: local variables can be freely accessed by functions defined inside their scope (see §2.6).

This is much the same rule as languages like JavaScript ("var foo;") and Perl ("my $foo;"), if that helps.

In the context of LuaUPnP, "global" means "global within that plugin's interpreter".  Each plugin gets its own Lua interpreter, so globals defined in one plugin's implementation file don't pollute others.