It is currently September 17th, 2021, 1:14 pm

Am I even on the right path.

Discuss the use of Lua in Script measures.
User avatar
Jkon
Posts: 185
Joined: December 4th, 2009, 2:05 am

Am I even on the right path.

Post by Jkon »

I've been using a slightly modified script by jsmorely to obtain the height of an image from a meter and return the value as a variable.The script works fine for one image but it is my intention to have up to nine images at once.I am trying to modify the script to accommodate this but I keep getting "ERROR: (01:02:24.086) Script: Script2.lua:25: 'end' expected (to close 'function' at line 14) near '<eof>'" and cant figure out why.

the following is the working script and then the dodgy one.Regardless of what's causing the current error I suspect my code is wrong any ways.Would really appreciate it if someone could point me in the right direction.

for 1 image

Code: Select all

PROPERTIES =
{

}

function Initialize()

	mtImage = SKIN:GetMeter("Image")

end -- function Initialize

function Update()

	Height = mtImage:GetH()

	SKIN:Bang("!SetVariable H1 "..Height)

	return tostring(Height)

end -- function Update
for multiple images(set to just 2 for now)

Code: Select all

PROPERTIES =
{

}

function Initialize()

	tImage = {}
	for i = 1, 2 do
	mtImage = SKIN:GetMeter("Image"..i)

end -- function Initialize

function Update()

	tHeight = {}
	for i = 1, 2 do
	tHeight[i] = mtImage:GetH()

	SKIN:Bang("!SetVariable H"..i " ".."\""..tHeight[i].."\"")

	return tostring(tHeight)

end -- function Update
I've also tried this approach without any luck.

Code: Select all

PROPERTIES =
{

}

function Initialize()

	tHeight = {}
	for i = 1, 2 do
	mtImage = SKIN:GetMeter("Image"..i)
	tHeight[i] = mtImage:GetH()

end -- function Initialize

function Update()

	SKIN:Bang("!SetVariable H"..i " ".."\""..tHeight[i].."\"")

return tostring(tHeight)

end -- function Update
Image
User avatar
jsmorley
Developer
Posts: 22270
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Am I even on the right path.

Post by jsmorley »

Right off, this:

mtImage = SKIN:GetMeter("Image"..i)

When in a loop like that, Is assigning all the meters to the same variable (or in this case to the same first cell of a table) over and over. So at the end, mtImage is going to be equal to what ever the LAST image meter is.

You need to:

tImages = {}

for i = 1, 2 do

tImages = SKIN:GetMeter("Image"..i)

end

See what I mean?

Then you have the "pointers" to the meters stored in a table, one per row. Now you can:


for i = 1, 2 do

Height = tImages:GetH()

SKIN:Bang("!SetVariable Meter"..i.."H".." "..Height)
-- sends "!SetVariable Meter1H xx" for the first loop...

end
User avatar
smurfier
Moderator
Posts: 1930
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Am I even on the right path.

Post by smurfier »

First, you need to end your for statements. Second, you're trying to assign multiple meters to the same variable.
function Initialize()

tImage,tHeight = {},{}
for i = 1, 2 do
tImage = SKIN:GetMeter("Image"..i)
end

end -- function Initialize
function Update()

for i = 1, 2 do
tHeight = mtImage:GetH()
SKIN:Bang("!SetVariable H"..i " ".."\""..tHeight.."\"")
end

return tostring(tHeight[1])

end -- function Update
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
Jkon
Posts: 185
Joined: December 4th, 2009, 2:05 am

Re: Am I even on the right path.

Post by Jkon »

Yes, and that seems quite obvious to me now that youve pointed it out.should have caught that myself.I'll make the changes and try again.thanks.
Image
User avatar
jsmorley
Developer
Posts: 22270
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Am I even on the right path.

Post by jsmorley »

Then you have the "pointers" to the meters stored in a table, one per row. Now you can:


for i = 1, 2 do

Height = tImages:GetH()

SKIN:Bang("!SetVariable Meter"..i.."H".." "..Height)

end

sends "!SetVariable Meter1H xx" for the first loop, "!SetVariable Meter2H xx" for the second.
User avatar
jsmorley
Developer
Posts: 22270
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Am I even on the right path.

Post by jsmorley »

I think smurfie and I are posting at the same time. Based on a quick look, I think we are more or less saying the same thing.

The only real differences are that I don't do quite as much "quoting" in the bang when there is no whitespace in the values, and you don't really need tHeight to be a table. It is fine as a plain variable as you are just reusing it during the loop. No real need to "store" it in a permanent way on each loop. Not in the context of how you are using it anyway.
User avatar
Jkon
Posts: 185
Joined: December 4th, 2009, 2:05 am

Re: Am I even on the right path.

Post by Jkon »

Okay.I'm having quote problems.As my variables are H1,H2 etc,i've tried changing your setvariable bang.

Code: Select all

PROPERTIES =
{

}

function Initialize()

   tImages = {}
   for i = 1, 2 do
   tImages[i] = SKIN:GetMeter("Image"..i)

end -- function Initialize

function Update()

   for i = 1, 2 do
   Height = tImages[i]:GetH()

   SKIN:Bang("!SetVariable H"..[i].." "..Height)

end

   return tostring(Height[i])

end -- function Update
I think i need to backslash something.
Image
User avatar
jsmorley
Developer
Posts: 22270
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Am I even on the right path.

Post by jsmorley »

No, it's my bad...

I had written it like that:

SKIN:Bang("!SetVariable H"...." "..Height)

It should be:

SKIN:Bang("!SetVariable H"..i.." "..Height)

The [] were careless....
User avatar
jsmorley
Developer
Posts: 22270
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Am I even on the right path.

Post by jsmorley »

You are still missing an "end" in that first loop in Initalize() though.

function Initialize()

tImages = {}

for i = 1, 2 do
tImages = SKIN:GetMeter("Image"..i)
end

end -- function Initialize
User avatar
Jkon
Posts: 185
Joined: December 4th, 2009, 2:05 am

Re: Am I even on the right path.

Post by Jkon »

Had to add another end function but it's working.
PROPERTIES =
{

}

function Initialize()

tImages = {}
for i = 1, 2 do
tImages = SKIN:GetMeter("Image"..i)

end

end -- function Initialize

function Update()

for i = 1, 2 do
Height = tImages:GetH()

SKIN:Bang("!SetVariable H"..i.." "..Height)

end

return tostring(Height)

end -- function Update


Also had to change the return to (Height).Not sure what the purpose of return is and what should really be in there.
Image