It is currently March 29th, 2024, 7:25 am

New to lua, image switching

Discuss the use of Lua in Script measures.
IDontKnow
Posts: 16
Joined: January 18th, 2012, 4:12 am

New to lua, image switching

Post by IDontKnow »

I'm trying to display different images and titles, and want to be able to toggle the url they are loaded from. I have no idea where to stat, and have the scroller in my skin, but I need something that catches if the variable goes above/below 9 and -1. I can't get it to work - there is an error near an '=' sign.... can I get some help?

Code: Select all

PROPERTIES=
{
--CurrentPage = "http://www.9gag.com"
post = SKIN:GetVariable("post1")
}

function Initialize()
post = SKIN:GetVariable("post1")
end

function Update()
CheckPost()
end

function CheckPost()
	if post > 9 then
		SKIN:GetVariable("post1")=1
		SKIN:GetVariable("post2")=2
	elseif post < 1 then
		SKIN:GetVariable("post1")=9
		SKIN:GetVariable("post2")=10
	end
end
I'm using post1 and post2 as skin variables to toggle the images, they change from 1-9.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: New to lua, image switching

Post by jsmorley »

IDontKnow wrote:I'm trying to display different images and titles, and want to be able to toggle the url they are loaded from. I have no idea where to stat, and have the scroller in my skin, but I need something that catches if the variable goes above/below 9 and -1. I can't get it to work - there is an error near an '=' sign.... can I get some help?

Code: Select all

PROPERTIES=
{
--CurrentPage = "http://www.9gag.com"
post = SKIN:GetVariable("post1")
}

function Initialize()
post = SKIN:GetVariable("post1")
end

function Update()
CheckPost()
end

function CheckPost()
	if post > 9 then
		SKIN:GetVariable("post1")=1
		SKIN:GetVariable("post2")=2
	elseif post < 1 then
		SKIN:GetVariable("post1")=9
		SKIN:GetVariable("post2")=10
	end
end
I'm using post1 and post2 as skin variables to toggle the images, they change from 1-9.
SKIN:GetVariable("post1")=1

SKIN:GetVariable() is used to retrieve a variable from a skin. Not to set one. To set a variable in a skin, you would use

if post > 9 then
SKIN:Bang(!SetVariable post1 1)
end

I have not seen any of your skin code, so I may be misunderstanding what you are trying to do, but I have a feeling you might be going down a path that won't end up where you want. Remember that plugins (like WebParser) cannot use DynamicVariables, so you cannot change the URL that they are pointing to on the fly. You can use a variable as the URL= for a WebParser measure, one that you set in the [Variables] section of a skin, but that is generally only to make it easier for a user to configure the URL they want for your skin. You can't use !SetVariable and have the WebParser measure go off somewhere else. It will only ever see the first value for the variable set when the skin is loaded or refreshed.
IDontKnow
Posts: 16
Joined: January 18th, 2012, 4:12 am

Re: New to lua, image switching

Post by IDontKnow »

then is it possible to use a .inc file for the variables, and set the url as a variable, then use the .lua to change the .inc text, and refresh the skin? That way, when the skin refreshes, it isn't resetting the url variable back to what it was, it's loading the modified document. Is that possible? and if so, how could I do that?
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: New to lua, image switching

Post by smurfier »

You can do that without using @include.

Just use !WriteKeyValue to write the value of the variable then use !Refresh to refresh the skin.
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: New to lua, image switching

Post by jsmorley »

IDontKnow wrote:then is it possible to use a .inc file for the variables, and set the url as a variable, then use the .lua to change the .inc text, and refresh the skin? That way, when the skin refreshes, it isn't resetting the url variable back to what it was, it's loading the modified document. Is that possible? and if so, how could I do that?
Yes, and you don't really need to use a .inc file, you can write directly to the skin .ini file if you want.

!WriteKeyValue [Section] [Key] [Value] ("FileSpec")

So something like:

!WriteKeyValue WebParserMeasureName URL "http://somesite.com" #CURRENTPATH##CURRENTFILE#

http://rainmeter.net/cms/Bangs

In Lua, you would need to get the values for #CURRENTPATH# and #CURRENTFILE# and #CURRENTCONFIG# in the Initialize() section with calls like:

sCurrentPath = SKIN:GetVariable('CURRENTPATH')
sCurrentFile = SKIN:GetVariable('CURRENTFILE')
sCurrentConfig = SKIN:GetVariable('CURRENTCONFIG')

Then in your Update() section get and set as a variable whatever the URL is you want to change.

sURLToSet = "http://SomeSite.com"

Then use the bang like:

SKIN:Bang('!WriteKeyValue WebParserMeasureName URL '..sURLToSet..' '..sCurrentPath..sCurrentFile)
SKIN:Bang('!Refresh '..sCurrentConfig)

The one caveat here is that you MUST be sure to do all this in a way that does not cause an endless loop of setting the URL and refreshing the skin. You can't have the skin when loaded or refreshed automatically get some values and run this script, then have the script set a value and refresh, if that is just going to cause the process to repeat endlessly.
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: New to lua, image switching

Post by smurfier »

I imagine something like this would work.

Lua.lua

Code: Select all

PROPERTIES={
UrlList=''
FilePath=''
}

function Initialize()
	URL,nUrl,cUrl={},0,SKIN:GetVariable('Url')
	for w in string.gmatch(PROPERTIES.UrlList,'[^%|]+') do table.insert(URL,w) end
	for a=1,#URL do if URL[a]==cUrl then nUrl=a; break end end
end

function Update()
	return #URL>0 and 'Success!' or 'Error!'
end

function Next()
	SKIN:Bang('!Execute [!WriteKeyValue Variables Url "'..URL[nUrl%#URL+1]..'" "'..PROPERTIES.FilePath..'"][!Refresh]')
end

function Previous()
	SKIN:Bang('!Execute [!WriteKeyValue Variables Url "'..URL[nUrl==#URL and 1 or nUrl-1]..'" "'..PROPERTIES.FilePath..'"][!Refresh]')
end
Skin.ini

Code: Select all

[Variables]
Url=http://Rainmeter.net

[Lua]
Measure=Script
ScriptFile=Lua.lua
UrlList=http://Rainmeter.net|http://rainmeter.deviantart.com
FilePath=#CURRENTPATH##CURRENTFILE#

[mPrevious]
Blah...
LeftMouseUpAction=!CommandMeasure "Lua" "Previous()"

[mNext]
Blah...
LeftMouseUpAction=!CommandMeasure "Lua" "Next()"
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: New to lua, image switching

Post by jsmorley »

Looks promising...

My only concern is that in Lua, I'm not sure that it will "default" to the skin's .ini file correctly on !WriteKeyValue if you don't specify it. Haven't tested, but I think I found in other cases that it pays to be specific when jumping back and forth from Lua.
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: New to lua, image switching

Post by smurfier »

Updated.
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
IDontKnow
Posts: 16
Joined: January 18th, 2012, 4:12 am

Re: New to lua, image switching

Post by IDontKnow »

I'm still getting a compiling error on the line that jsmorley gave me. Now it says that the error is near the '!' What am I doing wrong?

This is the right arrow image on the skin:

Code: Select all

[RightArrow]
Meter=Image
ImageName=arrow.png
ImageRotate=-90
Y=20
X=195
DynamicVariables=1
;LeftMouseUpAction=!Execute [!CommandMeasure PageCycler Update()]
LeftMouseUpAction=!Execute [!SetVariable post1 (#post1#+2)][!SetVariable post2 (#post2#+2)][!Update]
This is an updated .lua file I'm using:

Code: Select all

PROPERTIES=
{
--CurrentPage = "http://www.9gag.com"
post = 1;
}

function Initialize()
post = SKIN:GetVariable(post1)
end

function Update()
post = SKIN:GetVariable(post1)
CheckPost()
end

function CheckPost()
	if post >= 9 then
		SKIN:Bang(!SetVariable post1 1)
		SKIN:Bang(!SetVariable post2 2)
	elseif post =< 1 then
		SKIN:Bang(!SetVariable post1 9)
		SKIN:Bang(!SetVariable post2 10)
	end
end
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: New to lua, image switching

Post by smurfier »

The stuff inside SKIN:GetVariable() and SKIN:Bang() need to be strings. This means they need to be surrounded by either ' or ".

Code: Select all

function Initialize()
post = SKIN:GetVariable('post1')
end

function Update()
post = SKIN:GetVariable('post1')
CheckPost()
end

function CheckPost()
   if post >= 9 then
      SKIN:Bang('!SetVariable post1 1')
      SKIN:Bang('!SetVariable post2 2')
   elseif post <= 1 then
      SKIN:Bang('!SetVariable post1 9')
      SKIN:Bang('!SetVariable post2 10')
   end
end
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .