0

I'm trying to iterate over many variables at once, which are stored in a long array. To initialise them, I'm using a loop which goes over each of them, setting every variable to 0. By default, e.g. using: array[count] = 0 it will only change the value of the table's index. How do I set this variable from inside the table, as in getting the variable stored inside and changing it, not just the table's value at the given index?

4
  • 3
    I didn't understand what are you talking about. Some examples? Commented Oct 11, 2017 at 7:02
  • There are no reference types in Lua like in, e.g. C++ int &a = b. So once you copy something into the table it “forgets” where it came from and loses every connection to the original variable. Commented Oct 11, 2017 at 9:35
  • @HenriMenke Oh, well then is there an alternative way to iterate over variables? Commented Oct 11, 2017 at 13:41
  • @EgorSkriptunoff I have a table like so: arr = { var1, var2, var3 }, and I can't set them with arr[1] = 0 Commented Oct 11, 2017 at 13:41

1 Answer 1

2

You probably want to store fields in a Lua table:

a = { current = 4, first = 2, last = 10 }

Then you can set

a.current = 6

and also traverse all fields:

for k,v in pairs(a) do
   print(k,v)
end

or clear them with

for k in pairs(a) do
   a[k]=0
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.