local function AddTempIndex(a,b,c)
a[b] = c
delay(5, function() -- added
a[b] = nil
end)
end
Note that this function has a few bugs (or rather, it works exactly as written, nothing more or less, but you might not expect it to work the way it does)
Firstly, if you set a value, and set it again before it expires, then the value will become nil exactly 5 seconds after it was first set, but less than 5 seconds after it was set the second time.
local tbl = {}
AddTempIndex(tbl, "test", "hello")
print(tbl.test) -- hello
wait(2)
AddTempIndex(tbl, "test", "world")
print(tbl.test) -- world
wait(4)
print(tbl.test) -- nil
-- (test = world) expired in less than 5 seconds - in fact, it expired in 3 seconds
This bug can be fixed by never setting a key twice within 5 seconds
If you need to do that, then the code can be changed to make sure it’s not deleting what it didn’t set.
local function AddTempIndex(a,b,c)
a[b] = c
delay(5, function()
if a[b] == c then -- nothing else has changed this key in these 5 seconds, it has remained c
a[b] = nil
end -- else don't delete it, someone else set it after me
end)
end
local tbl = {}
AddTempIndex(tbl, "test", "hello")
wait(2)
AddTempIndex(tbl, "test", "world")
wait(4)
print(tbl.test) -- world
The bug remains if you try to set the same key to the same value twice within 5 seconds.
Either just don’t do that, or keep a tally of how many times each has been changed (even if it is changed to the same thing), and don’t nil it if (tally at the time the key was set) ~= (tally after 5 seconds)
Another “bug” is that it isn’t aware of arrays.
local tbl = {}
AddTempIndex(tbl, 1, "hello")
wait(1)
AddTempIndex(tbl, 2, "france,")
wait(1)
AddTempIndex(tbl, 3, "brazil,")
wait(1)
AddTempIndex(tbl, 4, "india")
print("Length of table:", #tbl)
print("Testing 1")
for i,v in ipairs(tbl) do print(i, v) end -- 1hello 2france, 3brazil, 4india
wait(2.5)
print("Length of table:", #tbl)
print("Testing 2 (ipairs)")
for i,v in ipairs(tbl) do print(i, v) end -- WHAT! NOTHING?
print("Testing 3 (pairs)")
for _,v in pairs(tbl) do print(i, v) end -- 2france, 3brazil, 4india
The reason is that an array is the part of the table whose keys are numbers 1, 2, 3 etc. starting from 1, and which does not include nil.
tbl[1]
would be nil at that time, so that is where the array ends, even if there is more after that nil.
The cause: that function does not shift the values back down like table.remove does, so gaps remain in the table.
Just adding table.remove to the function (replace a[b] = nil
with table.remove(a, c, b)
) will make it much, much buggier and worse. There is much more to it, and it can’t be programmed in one evening.
Using a metatable, it is possible to make a table use that function, without having to call it.
local tbl = CreateTempTable() -- make a special table
tbl.hello = "world!"
print(tbl.hello) -- world!
wait(6)
print(tbl.hello) -- nil
-- implementation
function AddTempIndex(a,b,c)
rawset(a, b, c) -- changed to avoid an infinite loop and crash
delay(5, function()
-- if a[b] == c then -- removed this, because keeping them in will introduce ANOTHER bug (__newindex does not run if index was not nil before, it runs on *new* index)
rawset(a, b, nil)
-- end
end)
end
local mt = {
__newindex = AddTempIndex -- literally that same function. it will be run any time a new value is created in a table whose metatable is this (mt)
}
function CreateTempTable()
return setmetatable({}, mt) -- make a new table and make its metatable the above one
end
I have not tested this code