PDA

View Full Version : Noob lua question


Emm
25-01-2007, 07:12 AM
I'm new to coding in lua and I have been stuck on this problem:

How do I use dynamic variables?

ex:
if the variable
X = Em

how do I make it so that

(the value of X) = 5

so that

Em = 5 ?

The value of X will change and i'm looking to store the 5 in whatever the value of X is. In php the way to do this is to use a double $$var which means the value of $var.

I know this is a simple question, but any help would be appreciated

Tunga
25-01-2007, 10:27 AM
Maybe I've misunderstood, are you just asking how to assign a value to a variable?

--Declare x and initialise to 5
local x = 5;

--Change value to 10
x = 10;

Wintrow
25-01-2007, 04:26 PM
No, he's asking how to store a reference of variable 'em' into variable 'x', so that when you assign a normal value to 'x', 'em' would also change:


local x, em;
foo_setref(x,em);
em = 2;
x = 5;
PrintValueToChat(em); -- results in printing '5' to chat instead of 2;


to answer your question: not like that no.

The closest thing are table variables:


local x, em;
em = { };
x = { };
x = em; -- This discards the empty table you made for x and assigns x to em's table
em.var1 = 2;
x.var1 = 5;
PrintToChat(em.var1); -- Will print 5 to chat instead of 2