Page 1 of 1

Create lua array from uservariable with multiple elements

Posted: Thursday 12 May 2016 18:48
by bickel
Hi,
Does anyone have an idea on how to fill a lua array with multiple elements from 1 uservariable containing these elements?

If i define the array in lua it works fine:

local radi={}
radi[1]={'string 1',number1,'something else'}
radi[2]={'string 2',number2,'something else'}
print(radi[1][1]) --output = string 1

Now if i put the values in a uservariable of type string and fill like this
In gui:
Uservar1 = {'string 1',number1,'something else'}
Uservar2 = {'string 2',number2,'something else'}

local radi={}
local i = 1
while uservariables["Radi"..i] do
radi=uservariables["Radi"..i]
i = i + 1
end
print(radi[1][1]) --outputs all elements including {} = {'string 1',number1,'something else'}

I also tried filling the array like this:

In gui:
Uservar1 = 'string 1',number1,'something else'
Uservar2 = 'string 2',number2,'something else'

radi={uservariables["Radi"..i]}

I think i can probably get it to work to read the uservar as string and then loop through al parts and recreate it into an array, but that's not as nice as i want it too.

Gr,
Ronald

Re: Create lua array from uservariable with multiple elements

Posted: Thursday 12 May 2016 22:58
by jmleglise
Hi,

What you want is called : serialization.

Have a look here : http://stackoverflow.com/questions/6075 ... -functions
and here : http://lua-users.org/wiki/TableSerialization

A nice way may be to serialize in Json with the common library : json.lua (encode / decode function).

Good luck :-)

Re: Create lua array from uservariable with multiple elements

Posted: Friday 13 May 2016 13:03
by bickel
Hi, tnx for your reply!
I surely can get this to work this way, i was only hoping for a shorter way to program this...some kind of oneliner, but i will start working on it this way so i can get on.

Gr,
Ronald

Re: Create lua array from uservariable with multiple elements

Posted: Friday 13 May 2016 17:06
by commodore white
Might be an idea to have a look a dzVents. It has the capability of storing variables from one invocation of your script to the next, and probably what you're looking for, the ability to share variables between scripts, so-called global variables.

You'll be able to put the complex structures in the variables that you're after. You should be able to mix-and match between regular LUA scripts and dzVents ones so you needn't recode everything. They work in parallel.

If you've got lots of scripts, you'll probably see a performance boost too.

Peter