PDA

View Full Version : Lua - information into a variable


elidion
20-02-2007, 08:28 AM
Okay, lets say I have some function that has...

myfunction(somevaraiblenum)

Now what I want to do would be something like what I have below...

function myfunction(num)
???num = someothervalue
end

What I am trying to get at is how do I make it be like

newvariablenum = someothervalue

so when my num=1 for example it would be newvariable1 = someothervalue

Please help :)

Thanks in advance.

elidion
20-02-2007, 08:52 AM
Wow that was helpful

Tunga
20-02-2007, 10:47 AM
Your original post makes no sense to me. Are you trying to create an array?

Duugu
20-02-2007, 12:20 PM
function myfunction(num)
if num == 1 then
local newvariable1;
newvariable1 = 12345678;
end
end

?

elidion
20-02-2007, 12:20 PM
sorry, I was trying to get a value that I got from a variable into the name of another variable, so for example if I had

counter = counter + 1

so I had this counter that would always increase by 1 each time some function was called. it would put that value into the name of another

Kind of similar to how people would reference

someframe_1
someframe_2

ect

I hope i'm still making sense

I wanted to do the same thing but with the names of the variables

so I would have

variable1 = whatevervalue
variablle2 = anothervalue

I pretty much ended up doing this with a table as I saw no other way.

Duugu
20-02-2007, 12:23 PM
values = {};

for x = 1, 1, 100 do
testfunction(x);
end

function testfunction(x)
values[x] = x;
end

? :)

Tunga
20-02-2007, 12:28 PM
Easiest way is to use an array, as above.

elidion
20-02-2007, 12:28 PM
Duugu kinda got the idea right, but I was trying to do this without a huge list of if and elseif statements.

because ultimately I wanted to have something like

for timer=1 counter, 1 do
if variable_timer == somevalue then
--Do something
end
end

So the part I am trying to get there is the timer value into the that variable name. so that I would have a variable_1 through variable_15 or whatever. I dont wanna have to put in a bunch of if statments for each value...but there might not be any other way.

elidion
20-02-2007, 12:36 PM
That might work but as I said I got it working with a table, it may not be best way, but I only have 10 frames so I only need 10 variables for the mod I am working on. I ended up making a queue file to go along with the mod to reserve spots in the table.

so when I call a function
myfunction(num)

I can have

function myfunction(num)
TABLE_NAME[num].time = GetTime()
end

it's at least working how I want it to :)

brykrys
22-02-2007, 06:19 PM
You could try using setglobal("variableroot"..num, value) to create a global variable.

Then getglobal("variableroot"..num) to read it back.

Of course, as I understand it, this is far slower and less efficient than using a table, and it adds more clutter to the global namespace :sad: so a table (preferably local) is probably your best option ... I'll go and shut up now :lipsrsealed:

Tunga
22-02-2007, 07:25 PM
Table seems fine yeah.