It is currently September 20th, 2019, 9:15 pm

Getting multiple Variables in a single command.

Discuss the use of Lua in Script measures.
User avatar
kyriakos876
Posts: 915
Joined: January 30th, 2017, 2:01 am
Location: Greece

Getting multiple Variables in a single command.

kyriakos876 » September 30th, 2017, 2:31 pm

Is there a way to get multiple Variables in lua, using one command?
Something like multipleVars = SKIN:GetVariable('B0','B1','B2')
User avatar
jsmorley
Developer
Posts: 19467
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Getting multiple Variables in a single command.

jsmorley » September 30th, 2017, 3:06 pm

No, there is not.
User avatar
jsmorley
Developer
Posts: 19467
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Getting multiple Variables in a single command.

jsmorley » September 30th, 2017, 3:35 pm

You can assign the value of multiple variables in one command in Lua, and you can at least "shorten" the overall length by using a function like this:

Code: Select all

function Initialize()

	myVar1, myVar2, myVar3, myVar4 = v('Var1'), v('Var2'), v('Var3'), v('Var4') 

end

function Update()

	print(myVar1, myVar2, myVar3, myVar4)

end

function v(varName)

	return SKIN:GetVariable(varName)

end
Which can be further shortened a bit with:

Code: Select all

function Initialize()

	function v(varName) return SKIN:GetVariable(varName) end
	myVar1, myVar2, myVar3, myVar4 = v('Var1'), v('Var2'), v('Var3'), v('Var4') 

end

function Update()

	print(myVar1, myVar2, myVar3, myVar4)

end
But by hook or by crook, you are going to have to distinctly call SKIN:GetVariable() for each one you want to get. There just is no SKIN:GetVariable('Var1', 'Var2', 'Var3', 'Var4'). I'm not sure I see any great value trying to jam it all in one command, but to each his own...
User avatar
kyriakos876
Posts: 915
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Getting multiple Variables in a single command.

kyriakos876 » September 30th, 2017, 4:51 pm

Yea, I decided that I don't want to call SKIN:GetVariables() 100 times so I made a table and stored them in
So, I have B0=1, B1=1, B2=1, ..., B100=1 and I get them into lua with:

Code: Select all

	multipleVars = {}	
	local i = 0
	while i < 100 do
	
		multipleVars [i] = SKIN:GetVariable(string.format('B%s', i))
		table.insert(multipleVars , multipleVars [i])
		
		if multipleVars [i] == '0' then break end 
		
		i = i + 1
		
	end
and then work from that table.

The if multipleVars [i] == '0' then break end is there because I'm searching 100 variables and stop when I find 0 in case you were wondering.(Obviously, I just mentioned it because it's irrelevant to the post.)
User avatar
jsmorley
Developer
Posts: 19467
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Getting multiple Variables in a single command.

jsmorley » September 30th, 2017, 5:16 pm

kyriakos876 wrote:Yea, I decided that I don't want to call SKIN:GetVariables() 100 times so I made a table and stored them in
So, I have B0=1, B1=1, B2=1, ..., B100=1 and I get them into lua with:

Code: Select all

	multipleVars = {}	
	local i = 0
	while i < 100 do
	
		multipleVars [i] = SKIN:GetVariable(string.format('B%s', i))
		table.insert(multipleVars , multipleVars [i])
		
		if multipleVars [i] == '0' then break end 
		
		i = i + 1
		
	end
and then work from that table.

The if multipleVars [i] == '0' then break end is there because I'm searching 100 variables and stop when I find 0 in case you were wondering.(Obviously, I just mentioned it because it's irrelevant to the post.)
Sounds fine. I'd probably shorten it a bit...

Code: Select all

   multipleVars = {}   
   for i = 1, 100 do
   
      table.insert(multipleVars , SKIN:GetVariable('B'..i))
      if multipleVars[i] == '0' then break end
      
   end
User avatar
kyriakos876
Posts: 915
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Getting multiple Variables in a single command.

kyriakos876 » September 30th, 2017, 5:24 pm

jsmorley wrote:Sounds fine. I'd probably shorten it a bit...

Code: Select all

   multipleVars = {}   
   for i = 1, 100 do
   
      table.insert(multipleVars , SKIN:GetVariable('B'..i))
      if multipleVars[i] == '0' then break end
      
   end
Yea... I'm still figuring out the syntax :P I'm gonna go with that then, as it's cleaner.
User avatar
kyriakos876
Posts: 915
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Getting multiple Variables in a single command.

kyriakos876 » September 30th, 2017, 5:36 pm

Is the i in the for loop temporary just for the loop? I mean, when I try to extract the i I'm getting nil. Should I save it in a variable before break if I want to use it somewhere else?
User avatar
jsmorley
Developer
Posts: 19467
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Getting multiple Variables in a single command.

jsmorley » September 30th, 2017, 5:42 pm

kyriakos876 wrote:Is the i in the for loop temporary just for the loop? I mean, when I try to extract the i I'm getting nil. Should I save it in a variable before break if I want to use it somewhere else?
The "i" is just for the loop. If you want to save it, you are back to your original method with i = i + 1 and all that.

I don't follow why you would care though. "i" is just going to end up as #multipleVars anyway.
User avatar
kyriakos876
Posts: 915
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Getting multiple Variables in a single command.

kyriakos876 » September 30th, 2017, 5:46 pm

Oh yea.... I already have it as the length... right...