It is currently March 29th, 2024, 5:12 am

Noob - trying to make simple Level script

Discuss the use of Lua in Script measures.
kennyist
Posts: 54
Joined: October 8th, 2011, 11:49 pm

Noob - trying to make simple Level script

Post by kennyist »

I'm trying to make a level script, So a meter gets a new level text depending on what "XP" you have from a Calc measure.

Code: Select all

PROPERTIES =
{

}
function Initialize()

	Level = SKIN:GetMeasure("Level1")
	
	
end -- function Initialize
	
function Update()

	XpSystem = SKIN:GetValue("XpSytem")

	if Xpsystem > 50 then
			SKIN:Bang('!SetOption "Level" "Text" "Level 1"')
	end
	
	if Xpsystem < 50 then
			SKIN:Bang('!SetOption "Level" "Text" "Level 2"')
	end
	
	if Xpsystem < 100 then
			SKIN:Bang('!SetOption "Level" "Text" "Level 3"')
	end
			
end -- function Update

I've never really done LUA before so im basing this off other ones and using the LUA page on rainmeter website. But on line 14 " XpSystem = SKIN:GetValue("XpSytem")" I get a "Attempted to call method 'GetValue' (a nil value)" error. Or when I try "XpSystem = SKIN:GetVariable("XpSytem")" I get "Attempt to compare number with nil" On the fist if statement.

The .ini code:

Code: Select all

[MeasureLuaScript]
Measure=Script
ScriptFile="#CURRENTPATH#Luatest.lua"

[XpSystem]
Measure=Calc
Formula=(AccountGiveawaysEntParse / 2) + AccountGiveawayswonParse * 2 + (AccountCommentsParse / 2)
DynamicVariables=1

[Level1]
Meter=String
DynamicVariables=1
x=40r
y=r
StringAlign=Left
FontColor=0,255,255,255
FontSize=10
Text=
AntiAlias=1
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Noob - trying to make simple Level script

Post by jsmorley »

kennyist wrote:I'm trying to make a level script, So a meter gets a new level text depending on what "XP" you have from a Calc measure.

Code: Select all

PROPERTIES =
{

}
function Initialize()

	Level = SKIN:GetMeasure("Level1")
	
	
end -- function Initialize
	
function Update()

	XpSystem = SKIN:GetValue("XpSytem")

	if Xpsystem > 50 then
			SKIN:Bang('!SetOption "Level" "Text" "Level 1"')
	end
	
	if Xpsystem < 50 then
			SKIN:Bang('!SetOption "Level" "Text" "Level 2"')
	end
	
	if Xpsystem < 100 then
			SKIN:Bang('!SetOption "Level" "Text" "Level 3"')
	end
			
end -- function Update

You have the right idea, but there are a couple of problems that are easily solved.

Code: Select all

function Initialize()

	msXP = SKIN:GetMeasure('XpSystem')
   
end -- function Initialize
   
function Update()

   valXP = msXP:GetValue()

	if valXP < 50 then
		SKIN:Bang('!SetOption Level1 Text \"Level 1\"')
	elseif valXP < 100 then 
		SKIN:Bang('!SetOption Level1 Text \"Level 2\"')
	else
		SKIN:Bang('!SetOption Level1 Text \"Level 3\"')
	end
         
end -- function Update
In Initialize() you want to get a handle to the measure you are going to get the value for later on each Update(). The measure in the skin that returns a value in this case is [XpSystem], so you want to set up a variable that is a handle to that measure. That is what I did with msXP = SKIN:GetMeasure('XpSystem').

Now on each Update(), you will retrieve the current value of that measure and put that in a variable that you can test. That is what valXP = msXP:GetValue() does. So now valXP will contain the number returned by the measure [XpSystem] in the skin.

Now you can do your testing and set the "Text" option of the meter in the skin. The meter in question is [Level1], so you just use SKIN:Bang('!SetOption Level1 Text \"Level 1\"') to set it. Note that you don't need quotes around each parameter to the bang, only when you are sending a value with "white space", in which case you send the quotes to Rainmeter, but need to "escape" them with the "\" char so they are not seen as quotes by the Lua, but only by Rainmeter.

Note: Don't use the PROPERTIES function at all anymore. It has been deprecated and is no longer needed.
kennyist
Posts: 54
Joined: October 8th, 2011, 11:49 pm

Re: Noob - trying to make simple Level script

Post by kennyist »

jsmorley wrote: You have the right idea, but there are a couple of problems that are easily solved.

Code: Select all

function Initialize()

	msXP = SKIN:GetMeasure('XpSystem')
   
end -- function Initialize
   
function Update()

   valXP = msXP:GetValue()

	if valXP < 50 then
		SKIN:Bang('!SetOption Level1 Text \"Level 1\"')
	elseif valXP < 100 then 
		SKIN:Bang('!SetOption Level1 Text \"Level 2\"')
	else
		SKIN:Bang('!SetOption Level1 Text \"Level 3\"')
	end
         
end -- function Update
In Initialize() you want to get a handle to the measure you are going to get the value for later on each Update(). The measure in the skin that returns a value in this case is [XpSystem], so you want to set up a variable that is a handle to that measure. That is what I did with msXP = SKIN:GetMeasure('XpSystem').

Now on each Update(), you will retrieve the current value of that measure and put that in a variable that you can test. That is what valXP = msXP:GetValue() does. So now valXP will contain the number returned by the measure [XpSystem] in the skin.

Now you can do your testing and set the "Text" option of the meter in the skin. The meter in question is [Level1], so you just use SKIN:Bang('!SetOption Level1 Text \"Level 1\"') to set it. Note that you don't need quotes around each parameter to the bang, only when you are sending a value with "white space", in which case you send the quotes to Rainmeter, but need to "escape" them with the "\" char so they are not seen as quotes by the Lua, but only by Rainmeter.

Note: Don't use the PROPERTIES function at all anymore. It has been deprecated and is no longer needed.
Alright, Saw the Depreciated code in another tutorial skin.

EDIT: Ok me not thinking right, Putting greater than in the wrong place in my mind
Ok yeah it works with that, but im trying to work out how the values would work in related to what i want. This is how for example I want the levels to progress:
L1= 1- 50
L2= 50- 100
L3=100- 200
L4=200 -400
L5=400 -600
L6=600-800

and so on.

I've tried doing that with somones account who adds up to 106 points, But they end up as level 4:

Code: Select all

   valXP = msXP:GetValue()

   if valXP < 1 then
      SKIN:Bang('!SetOption Level1 Text \"Level 1\"')
   elseif valXP < 50 then
      SKIN:Bang('!SetOption Level1 Text \"Level 2\"')
   elseif valXP < 100 then
      SKIN:Bang('!SetOption Level1 Text \"Level 3\"')
   elseif valXP < 200 then
      SKIN:Bang('!SetOption Level1 Text \"Level 4\"')
   elseif valXP < 400 then
      SKIN:Bang('!SetOption Level1 Text \"Level 5\"')
   elseif valXP < 600 then
      SKIN:Bang('!SetOption Level1 Text \"Level 6\"')
   else
      SKIN:Bang('!SetOption Level1 Text \"Level 7\"')
   end


How would you do this? Or am I really going wrong again.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Noob - trying to make simple Level script

Post by jsmorley »

Yeah, you could use something like:

Code: Select all

   if valXP <= 50 then
      SKIN:Bang('!SetOption Level1 Text \"Level 1\"')
   elseif valXP <= 100 then
      SKIN:Bang('!SetOption Level1 Text \"Level 2\"')
   elseif valXP <= 200 then
      SKIN:Bang('!SetOption Level1 Text \"Level 3\"')
   elseif valXP <= 400 then
      SKIN:Bang('!SetOption Level1 Text \"Level 4\"')
   elseif valXP <= 600 then
      SKIN:Bang('!SetOption Level1 Text \"Level 5\"')
   elseif valXP <= 800 then
      SKIN:Bang('!SetOption Level1 Text \"Level 6\"')
   else
      SKIN:Bang('!SetOption Level1 Text \"Level 7\"')
   end