r/lua • u/CartoonistNo6669 • 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.
3
u/Spartelfant Jun 09 '25
Strings in Lua are immutable values. You cannot alter a string, as you can in for examle C. Instead a new string with the desired modifications is created.
This is because of the way Lua implements strings. All strings in Lua are internalized. This means that Lua keeps a single copy of any string. Whenever a new string appears, Lua checks whether it already has a copy of that string and, if so, reuses that copy.
So what happens when you do
['VarToChange'] = TestVariableis that['VarToChange']is pointed at the same string thatTestVariableis pointing at.Then when you do
TestVariable = 456, Lua checks if it already has a copy of this string, or creates it if necessary, and then pointsTestVariableat this new string.Meanwhile
['VarToChange']is still pointing at the same immutable string as before.5
u/Previous-Traffic-130 Jun 09 '25 edited Jun 09 '25
Because it saves its value at the time, its not a reference to the variable
to do that either do
or
2
u/Logical_Strike_1520 Jun 09 '25 edited Jun 09 '25
They’re both variables.. Also you have this thought process backwards imo.
TestObject.VarToChange = newValue;
Is wayyyy more readable and less prone to bugs than
TestVal = newValue; -> mutates TestObj.
ETA: If you tell me what exactly it is you want to achieve I can probably help you out. Your example doesn’t give much context though.
4
u/PhilipRoman Jun 09 '25
If you're fine with metatable based solutions, you can do this:
This way, all tables which have such a metatable will share the same
VarToChangeIf you omit the
__newindex, then assigning to a table'sVarToChangewill override it for that table, but other tables will keep the shared one.But the "correct" way would be to just use functions instead of table accesses. Lua is a pass-by-value language and variables are not meant to magically propagate to all tables.
3
u/Motor_Let_6190 Jun 09 '25
Simple types (not a table) are passed by value in Lua.
2
u/[deleted] Jun 09 '25 edited Jun 09 '25
Sadly, what you want cannot be done with lua simply, lua copies values by default, there are no references (except tables), unless you use metatables can you achieve it via fallbacks.
FWIW You can do what you want using the debug library and messing with the local scope. This is not recommended. At least with your example, you're better off doing something like this
Now, its not really nice to do this this way, but you could use _ENV (or setenv if you're on luajit)
This method clears up variable creation somewhat, but its also tedious because you now need to localize print and EVERYTHING in _G you're going to use, so its better to put _ENV in a do end block. its also a good idea that _ENV's metatable __index points to _G as well. so you avoid all that pointless localization.
2
u/TomatoCo Jun 09 '25
You could do something like
1
u/AutoModerator Jun 09 '25
Hi! Your code block was formatted using triple backticks in Reddit's Markdown mode, which unfortunately does not display properly for users viewing via old.reddit.com and some third-party readers. This means your code will look mangled for those users, but it's easy to fix. If you edit your comment, choose "Switch to fancy pants editor", and click "Save edits" it should automatically convert the code block into Reddit's original four-spaces code block format for you.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
0
u/redditbrowsing0 Jun 09 '25
It doesn't automatically change the value.