LOL08083
(LOL08083)
January 20, 2022, 5:20pm
1
You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I want to declare a “key” using another “key” from the same table
What is the issue? Include screenshots / videos if possible!
I can’t declare a “key” table to use something from the same table
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I tried this
local someTable = {
["Seconds"] = 120
["Minutes"] = self.Seconds/60
}
and this
local someTable = {
["Seconds"] = 120
["Minutes"] = someTable.Seconds/60
}
The only easy way I can think to do this is to define them out of the table:
local someTable = {}
someTable.Seconds = 120
someTable.Minutes = someTable.Seconds/60
You could use meta tables, but that’s just over engineering it.
This won’t work, firstly, this will say something along the lines of Attempt to index nil with seconds when you add the semi-colons, or commas at the end of the lines (otherwise it won’t run at all). And if you initialize someTable to {}, you’ll get Attempt to perform arithmetic on nil value.
You can see that here:
1 Like
Forummer
(Forummer)
January 20, 2022, 11:48pm
5
local dictionary = {
["Seconds"] = 120
}
local metadictionary = {
["__index"] = function(dict, key)
if key == "Minutes" then
return dict["Seconds"]/60
end
end
}
setmetatable(dictionary, metadictionary)
print(dictionary["Minutes"]) --prints 2