PDA

View Full Version : Executing LUA script on variable change



Ginsonic
July 12th, 2005, 10:55 PM
ZoomPlugin sets a variable ZP.PlayState. Is there any possibility to execute a user written LUA function when this variable changes, so I can set a user defined variable depending on the value of ZP.PlayState.

Thanks in advance for Your help !

Rob H
July 12th, 2005, 11:18 PM
Yes, in your CCF's .lua file add the following to the OnCCFLoad() function



RegisterVariableWatch('ZP.PlayState', myPlayStateFunc)


And write a function called myPlayStateFunc(varName, value) that does what you need with the play state.

NB the 'old' way to do this was using the OnVariableChange() function, but direct use of this is now deprecated.[/code]

Ben S
July 13th, 2005, 02:48 AM
Rob is correct about the new way to do this, but this requires a newer version of NetRemote than the version available via the main "Download Trials" link off the website. Check out the prerelease thread in this forum to download a version that supports Rob's way, which will become the standard.

Ginsonic
July 13th, 2005, 04:25 AM
Rob and Ben,

Many thanks for Your fast help !

@Rob: Works like a charm !

@Ben: I always try to download the actual version, so Rob's procedure worked at once :wink:

Thanks again, regards
Dieter

Jlee
July 17th, 2005, 11:48 PM
I hadn't realised that there's a new way to watch variable changes. Why has it changed? What's the advantage of the new way? Should I be converting my CCF to the new way? There used to be an issue with having too much in OnCCFLoad so I migrated quite a lot of stuff out of it into OnVariableChange.

Questions, questions, questions :D

Rob H
July 17th, 2005, 11:58 PM
There are a couple of advantages :-

1) It should be faster - even with the current implementation. If you have multiple variables to watch, every time any variable changes then NR has to run through a whole load of comparisons. With my VariableWatch script it's just a single table lookup. And when Ben finally integrates it into NR it'll be a lot faster since it will only be called when the variable you are interested in changes.

2) It makes it easier to use other peoples' Lua scripts that also watch variables. With OnVariableChange you'd have to edit your own OnVariableChange to incorporate their code since you can only have one OnVariableChange function.

Jlee
July 18th, 2005, 01:30 AM
Thanks Rob, that makes perfect sense. Couple of questions:

1. So if Ben hasn't intergrated your code into NR what do I need to install to be able to use this new method?

2. Just to clarify how this works, here's an extract from my OnVariableChange section and what I think it will look like when changed to the new way. Please could you just confirm I have it right before i go making all these changes.


--Old way....
function OnVariableChange(varname,varvalue)
if (varname=="MP.Lyrics") then
if (string.len (varvalue) > 50) then
NetRemote.SetVariable("LYRICSAVL","1");
else
NetRemote.SetVariable("LYRICSAVL","0");
end;
end;

if (varname == "cc.icon") then
iconnumber = tonumber(NetRemote.GetVariable("cc.icon"));
setweatherimg();
end;
end;

--New way....
function OnCCFLoad()
RegisterVariableWatch('MP.Lyrics', lyricsFunc)
RegisterVariableWatch('cc.icon', iconFunc)
end;

function lyricsFunc()
if (string.len (MP.Lyrics) > 50) then
NetRemote.SetVariable("LYRICSAVL","1");
else
NetRemote.SetVariable("LYRICSAVL","0");
end;
end;

function iconFunc()
iconnumber = tonumber(NetRemote.GetVariable("cc.icon"));
setweatherimg();
end;


Thanks for your help.

avid
July 18th, 2005, 02:02 AM
Just to dive in, cos Rob hasn't yet ...


1. So if Ben hasn't intergrated your code into NR what do I need to install to be able to use this new method?
AFAIK, you don't need to install anything, Ben has added the necessary LUA to 1.0.37. Rob's "Ben hasn't intergrated ..." refers to a future more efficient implemntation of RegisterVariableWatch directly in NR. Rob's current LUA sript still uses OnVariableChange behind the scenes and so gets called on every change of every variable.


Please could you just confirm I have it right before i go making all these changes.
The functions (lyricsFunc etc) still take varname,varvalue parameters like OnVariableChange. This is important as the varvalue is the new value, whereas NetRemote.GetVariable yields the old (pre-changed) value.

Also note that your lyricsFunc mixes up NR and LUA varibles - a classic mistake!

I hope this helps

Brian

Rob H
July 18th, 2005, 02:22 AM
Brian pretty much seems to have covered everything I could have said :)

Jlee
July 18th, 2005, 03:11 AM
Ok, thanks for that.

The functions (lyricsFunc etc) still take varname,varvalue parameters like OnVariableChange. This is important as the varvalue is the new value, whereas NetRemote.GetVariable yields the old (pre-changed) value.

Also note that your lyricsFunc mixes up NR and LUA varibles - a classic mistake!

I'll have another go at the code then. I'm not totally sure what I'm doing and I can't get my head round why NetRemote.GetVariable should yield the old value. Please could you confirm if I'm getting warmer. I've commented out the 4 lines I changed since my last effort and put the new lines below them.


function OnCCFLoad()
RegisterVariableWatch('MP.Lyrics', lyricsFunc)
RegisterVariableWatch('cc.icon', iconFunc)
end;

--function lyricsFunc()
function lyricsFunc(varname,varvalue)
-- if (string.len (MP.Lyrics) > 50) then
if (string.len (varvalue) > 50) then
NetRemote.SetVariable("LYRICSAVL","1");
else
NetRemote.SetVariable("LYRICSAVL","0");
end;
end;

--function iconFunc()
function iconFunc(varname,varvalue)
-- iconnumber = tonumber(NetRemote.GetVariable("cc.icon"));
iconnumber = tonumber(varvalue);
setweatherimg();
end;

avid
July 18th, 2005, 03:21 AM
Look plausible to me - give it a go

Brian

Jlee
July 18th, 2005, 04:05 AM
Thanks I will. I guess I should have a go at migrating to GAC+ at the same time. Oh what fun!.