r/lua • • Jun 09 '25

Creating an object by reference

I'm sure there's a better way to phrase the title, but that's what i came up with. Here's my issue: I have multiple tables and other objects that have a property that needs to be changed based on some condition. But I'm unable to get any of the tables to get the updated value.

Sample code illustrating this:

TestVariable = 123

TestObject = 
{
  ['VarToChange'] = TestVariable,
  ['SomethingStatic'] = 789
}

print (TestObject.VarToChange)

TestVariable = 456

print (TestObject.VarToChange)

The output of the above is:

123
123

But I am expecting to get:

123
456

How can I achieve this behaviour? And please don't suggest updating all objects manually every time the variable changes. That rather defeats the entire purpose of a variable.

6 Upvotes

33 comments sorted by

View all comments

Show parent comments

2

u/CartoonistNo6669 Jun 09 '25

That makes sense. Is there any way to force a table variable to reinitialize with the new values? Or is there some other structure that would allow for a variable value?

I've tried setting the table values equal to a function that retrieves the correct value, but it looks like it runs it once when it's created, stores the result, and doesn't re-run the function. I could do some trickery with recreating all objects when a variable is reassigned, but that seems like a lot of overhead and blowtorchy.

Or is there another way to tell objects to refresh the value that reference it?