A quick question:
I want to create a dictionary with a metatable that changes itself to another value once fired.
Is it possible to just do
Dictionary = "something here"
or do I need to do some other workaround?
A quick question:
I want to create a dictionary with a metatable that changes itself to another value once fired.
Is it possible to just do
Dictionary = "something here"
or do I need to do some other workaround?
Not sure what you mean. Do you mean you want to create a metatable that when a metamethod is fired, it changes the value inside of the metatable?
local init = {
['__IndexAccumulations'] = 0;
['__NewIndexAccumulations'] = 0;
}
local meta = {
__index = function(tbl, index)
tbl['__IndexAccumulations'] += 1 -- this would increase the __IndexAccumulations value by 1 when the __index metamethod is fired
end;
__newindex = function(tbl, index, key)
tbl['__NewIndexAccumulations'] += 1
end;
}
return setmetatable(init, meta)
You can also just do this to create a new value inside of the init table:
local init = {}
local meta = {
__newindex = function(tbl, index, key)
tbl[index] = key
end;
}
return setmetatable(init, meta)
Or if you want to create a function like :CreateProperty, you wouldn’t even need metatables
local init = {
['CreateProperty'] = function(self, property, value)
self[property] = value
end;
}
init:CreateProperty('CoolProperty', 1)
print(init.CoolProperty) --> 1
I mean like, Changing the value of the entire table itself.
Dictionary = setmetatable({}, {
__index = function(self, Index) self = nil end
})
Something along those lines.
Like destroying it? I don’t think that’s possible with an integrated method. But you can always just iterate through the table and get the values without the metatable and create a new blank table without the metatable. Same with the metatable but you’d have to use getmetatable
. I’m just not sure what you mean as the first argument of __index and __newindex is a reference to the table but the table’s value itself can’t be changed as it’s a different datatype.