learning metatables from a youtube tutorial. im not sure why it prints 20 for the first and 4th for the next. my intention is for it to print 24 for the 2nd print. ive copied exactly for the second time, but i still cannot achieve the desired output
local mytable = {}
mytable.value = 20
local metatable = {}
metatable.__add = function(t,v)
t.value = v
return t
end
setmetatable(mytable, metatable)
print(mytable)
mytable += 4
print(mytable)
I don’t deal with metatables, but it seems it is doing exactly what you coded it to do - it is setting the value of mytable to the value that you ‘added’ to it, being 4. If you make make t.value = v be t.value += v, does it give the desired output?