Getting multiple Variables in a single command.

Discuss the use of Lua in Script measures.
User avatar

Getting multiple Variables in a single command.

September 30th, 2017, 2:31 pm
kyriakos876
   [276 posts]

Is there a way to get multiple Variables in lua, using one command?
Something like multipleVars = SKIN:GetVariable('B0','B1','B2')
User avatar

Re: Getting multiple Variables in a single command.

September 30th, 2017, 3:06 pm
jsmorley
Developer   [16251 posts]

No, there is not.
User avatar

Re: Getting multiple Variables in a single command.

September 30th, 2017, 3:35 pm
jsmorley
Developer   [16251 posts]

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

Re: Getting multiple Variables in a single command.

September 30th, 2017, 4:51 pm
kyriakos876
   [276 posts]

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

Re: Getting multiple Variables in a single command.

September 30th, 2017, 5:16 pm
jsmorley
Developer   [16251 posts]

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

Re: Getting multiple Variables in a single command.

September 30th, 2017, 5:24 pm
kyriakos876
   [276 posts]

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

Re: Getting multiple Variables in a single command.

September 30th, 2017, 5:36 pm
kyriakos876
   [276 posts]

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

Re: Getting multiple Variables in a single command.

September 30th, 2017, 5:42 pm
jsmorley
Developer   [16251 posts]

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

Re: Getting multiple Variables in a single command.

September 30th, 2017, 5:46 pm
kyriakos876
   [276 posts]

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

Return to “Lua Scripting”



Who is online

Users browsing this forum: No registered users and 1 guest