It is currently March 28th, 2024, 12:23 pm

Getting multiple Variables in a single command.

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

Getting multiple Variables in a single command.

Post by kyriakos876 »

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: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Getting multiple Variables in a single command.

Post by jsmorley »

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

Re: Getting multiple Variables in a single command.

Post by jsmorley »

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: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Getting multiple Variables in a single command.

Post by kyriakos876 »

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: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Getting multiple Variables in a single command.

Post by jsmorley »

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: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Getting multiple Variables in a single command.

Post by kyriakos876 »

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: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Getting multiple Variables in a single command.

Post by kyriakos876 »

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: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Getting multiple Variables in a single command.

Post by jsmorley »

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: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Getting multiple Variables in a single command.

Post by kyriakos876 »

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