It is currently April 25th, 2024, 1:31 am

[Solved] Error: Attempt to call method 'Bang' (a nil value)

Discuss the use of Lua in Script measures.
Nooby4Ever
Posts: 29
Joined: December 22nd, 2009, 2:49 pm

[Solved] Error: Attempt to call method 'Bang' (a nil value)

Post by Nooby4Ever »

Hallo

This appears in the rainmeter about - log console

Code: Select all

Error Script: Battery.lua:30: attempt to call method 'Bang' (a nil value)
It's my first time i try lua script and i'm stuck with this error, my code:

Code: Select all

PROPERTIES = {}
	-- Prefixes, not a part of the syntax:
	-- s(string)
	-- i(integer/number)
	-- ms(measure)
	-- mt(meter)
	-- t(table)
	
function Initialize()

	msPowerPercent = SKIN:GetMeasure("mPowerPercent")
	msPowerSource = SKIN:GetMeasure("mPowerSource")
	
	meter1 = SKIN:GetMeter("meterBGIcon7")
	meter2 = SKIN:GetMeter("meterBattGraph")
	
end

function Update()
	iPowerPercent = msPowerPercent:GetValue()
	sPowerSource = msPowerSource:GetStringValue()
	
	if sPowerSource == "Battery" then
		if iPowerPercent <= 10 then 
			meter1:Bang("!SetVariable ImageName BatteryOut.png")
		else 
			meter1:Bang("!SetVariable ImageName Battery.png")
		end
	else
		meter1:Bang("!SetVariable ImageName BatteryPlug.png")
	end
	
	meter1:Bang("!Refresh")	
		
	if iPowerPercent <=10 then 	   meter2:Bang("!SetVariable BarColor 0,92,0,255")
    elseif iPowerPercent <=30 then meter2:Bang("!SetVariable BarColor 92,0,0,255")
    elseif iPowerPercent <=50 then meter2:Bang("!SetVariable BarColor 0,0,92,255")
    elseif iPowerPercent <=70 then meter2:Bang("!SetVariable BarColor 11,92,0,255")
    elseif iPowerPercent <=90 then meter2:Bang("!SetVariable BarColor 0,92,11,255")
    else                 		   meter2:Bang("!SetVariable BarColor 125,92,48,255")
    end
	
	meter2:Bang("!Refresh")		
	
	return iPowerPercent	
end
The error is called on line 30, that would be meter1:Bang("!SetVariable ImageName BatteryPlug.png") (which is the line the script should come to with the current measures in my skin).

What did i do wrong?
(Running rainmeter 2.20 r1116 64bit)
Last edited by Nooby4Ever on January 14th, 2012, 11:40 am, edited 2 times in total.
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Error: Attempt to vall method 'Bang' (a nil value)

Post by smurfier »

The Bang command does not work that way.

The correct usage is: SKIN:Bang('!SetVariable ImageName BatteryPlug.png')

If you're trying to change the ImageName of meterBGIcon7, then it's:
SKIN:Bang('!SetOption meterBGIcon7 ImageName BatteryPlug.png')
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 . . .
Nooby4Ever
Posts: 29
Joined: December 22nd, 2009, 2:49 pm

Re: Error: Attempt to vall method 'Bang' (a nil value)

Post by Nooby4Ever »

smurfier wrote:The Bang command does not work that way.

The correct usage is: SKIN:Bang('!SetVariable ImageName BatteryPlug.png')

If you're trying to change the ImageName of meterBGIcon7, then it's:
SKIN:Bang('!SetOption meterBGIcon7 ImageName BatteryPlug.png')
Indeed that was my intention, thanks.
So just to make sure i get it right, SKIN is a static variable initilized by "rainmeter" itself that hold the current skin name/path/whatever.

I see were i gone wrong :? , i got this from the website:

Code: Select all

anyMeter = SKIN:GetMeter("ExampleMeter")
anyMeter:SetX(25)
But (now i saw it again) it indeed doesn't work for Bangs
Should have found this out myself, and i had used " instad of ' :(
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Error: Attempt to vall method 'Bang' (a nil value)

Post by jsmorley »

Nooby4Ever wrote: Indeed that was my intention, thanks.
So just to make sure i get it right, SKIN is a static variable initilized by "rainmeter" itself that hold the current skin name/path/whatever.

I see were i gone wrong :? , i got this from the website:

Code: Select all

anyMeter = SKIN:GetMeter("ExampleMeter")
anyMeter:SetX(25)
But (now i saw it again) it indeed doesn't work for Bangs
Should have found this out myself, and i had used " instad of ' :(
Without delving too deep into the innards of how Lua works under the covers, It probably isn't good to think of SKIN as a "variable" in this case (or anyMeter in the example either for that matter) as they really are not. They are more like "objects" and the things after the ":" (like SKIN:Bang() or anyMeter:SetX()) are more like "methods". The SKIN object does indeed represent the skin that called the lua script, but be careful not to try to use it like a variable, you will just get some "handle" to the object.
Nooby4Ever
Posts: 29
Joined: December 22nd, 2009, 2:49 pm

Re: Error: Attempt to call method 'Bang' (a nil value)

Post by Nooby4Ever »

Ok thanks for the explaination (my word choice was indeed not very good, not that i knew it's an "object", i ment more a sort of sytax word as print but without doing something just pointing to the skin)
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Error: Attempt to call method 'Bang' (a nil value)

Post by jsmorley »

Nooby4Ever wrote:Ok thanks for the explaination (my word choice was indeed not very good, not that i knew it's an "object", i ment more a sort of sytax word as print but without doing something just pointing to the skin)
Fair enough, and my syntax is not meant to be technically literally correct either. I just wanted to point out that at least for me, thinking of those things as objects and methods for those objects helps me keep what is going on a bit clearer in my own head. It is sorta consistent with how the real geeks (I exclude myself) describe "object oriented" in the context of Lua.

http://www.lua.org/pil/16.html