So basically I have a table inside another table. That table has a metatable attached to it and the function starts there. I need to find a way to get the key of the table that same metatable is attached to, from one of the metatable’s functions
Sounds like you are putting objects in a table like as objects in Lua can be presented as a table with a metatable sample car object:
Sample car object example with metatable
Car = {}
Car.__index = Car
function Car.new(position, driver, model)
local newcar = {}
setmetatable(newcar, Car)
newcar.Position = position
newcar.Driver = driver
newcar.Model = model
return newcar
end
function Car:Boost()
self.Speed = self.Speed + 5
end
function Car:PrintKey()
print(self.Name.." is inside garage number: "..self.Index)
end
Moreover, you want these objects to know the key of the table it’s nested inside I believe.
local randomTable = {Object1,Object2}
--Object 1 is inside this table with key = 1
--Object 2 is inside this table with key = 2
Unfortunately, from my limited knowledge of metatables, I believe there is no way for object 1 to know it’s nested inside the table unless we give the information to object 1 directly like Object1.Index = 1 . However, I believe it’s possible to do this process automatically using Metamethods like so by tracking the parent table using the sample car object such that it should fit your requirements of the finding:
Stolen from this metamethod tutorial.
local Car = require(script.Car)
local jeep = Car.new()
jeep.Name = "Jeep"
local function TrackDownTable()
local t = {}
local proxy = setmetatable({}, {
__index = function(_, key)
return t[key]
end,
__newindex = function(_, key, value)
value.Index = key
t[key] = value
end
})
return proxy
end
local Garage = TrackDownTable()
Garage[1] = jeep
jeep:PrintKey()
Garage[5] = jeep
jeep:PrintKey()
Garage["VIP_garage"] = jeep
jeep:PrintKey()
Print statements:
Jeep is inside garage number: 1
Jeep is inside garage number: 5
Jeep is inside garage number: VIP_garage
I wonder if there is a better solution other than manually storing the identifying index like so within that table.
Would there at least be a way to know what the key to access that table is or would I have to explicitly give that information to the method?
To visualize it:
local tables = {Key1 = {}, Key2 = {}}
local metatable = {
__newindex = function(this, key, value)
--I want to know the key here
end
}
tables.Key1 = setmetatable({}, metatable)
To summarize this, I would want the method with the comment to know that the key to this
is Key1
.
To be fair I’ve already found a way to feed it the information, but I still would like to know if there is any way to do what I ask for the future.
Eh, sorry the only way I know how is to do a reverse key value pair search for your visualization
local tables = {Key1 = {}, Key2 = {}}
local metatable = {
__newindex = function(this, key, value)
--I want to know the key here
for i,v in pairs(tables) do
if this == v then
print(i)
end
end
end
}
tables.Key1 = setmetatable({}, metatable)
tables.Key1[2] = 5
Print statement:
Key1
However, then you would need to find the place where Key1={} is nested in. The only solution I found is to use a linked list from this scripting helper post from a post to find the Parent of a table.
Yeah, there is no way to find the parent of a table from a quote I found on the forum which I think makes sense:
Tables don’t have parents, because they are not instances in a game. So what I mean is you would access something in the game by saying script.Parent or game.ServerStorage or game.Lighting.
Tables don’t store where they are stored by themselves from this statement if it’s true which I believe it is.