Search code examples
luacoronasdklove2dluajitentity-component-system

How can I get a reference of a table index?(lua)


I want to get a reference to something in a table, but when i try:

myReferences[x] = table[i]

I copy the value. I tried using a variable between the tables:

local myReference = table[i]
myReferences[x] = myReference

but that does not work either

Thanks in advance :)


Solution

  • There is no "reference" thing in Lua. When you assign something - that only binds new value to a variable or to a table's element.

    If you need a reference to a table's element, you need the table itself and the value of referencing index stored together. That pair will be your equivalent for a reference. For convenience it may be wrapped in some kind of a functional object, but essentially it would be the same pair.

    But as noted in comments, it's likely that you're trying to solve some other task in a c++-ish way, so you'd better explain what you're trying to achieve.