lua - Error while useing a Method of a Metatable 'attempt to call method (a nil value)' -


i error 'attempt call method 'dot' (a nil value)' while running following code:

-- vector2 class vector2 = {x = 0, y = 0, magnitude = 0, unit = nil} function vector2.new(xvalue,yvalue)     local tmp = {}     setmetatable(tmp,vector2)     tmp.x = xvalue     tmp.y = yvalue      tmp.magnitude = math.sqrt(tmp.x * tmp.x + tmp.y * tmp.y)     if tmp.magnitude ~= 1         tmp.unit = tmp/tmp.magnitude     else         tmp.unit = tmp     end      return tmp end  -- arithmetic function vector2.__add(a,b)     if getmetatable(a) == getmetatable(b)         return vector2.new(a.x+b.x, a.y+b.y)     end end  function vector2.__sub(a,b)     if getmetatable(a) == getmetatable(b)         return vector2.new(a.x-b.x, a.y-b.y)     end end  function vector2.__mul(a,b)     if tonumber(b) ~= nil         return vector2.new(a.x*b, a.y*b)     end end  function vector2.__div(a,b)     if tonumber(b) ~= nil         return vector2.new(a.x/b, a.y/b)     end end  function vector2.__unm(tmp)     return vector2.new(-tmp.x, -tmp.y) end  -- comparisons function vector2.__eq(a,b)     if getmetatable(a) == getmetatable(b)         if a.x == b.x , a.y == b.y             return true         else             return false         end     end end  function vector2.__lt(a,b)     if getmetatable(a) == getmetatable(b)         if a.magnitude < b.magnitude             return true         else             return false         end     end end  function vector2.__le(a,b)     if getmetatable(a) == getmetatable(b)         if a.magnitude <= b.magnitude             return true         else             return false         end     end end  -- functionals function vector2.__tostring(tmp)     return tmp.x..", "..tmp.y end  function vector2:dot(b)     return self.x*b.x + self.y*b.y end  function vector2:lerp(b,amn)     return self + (b-self)*amn end  print(vector2.new(1,0.3).magnitude) print(vector2.new(1,0):dot(vector2.new(0,1))) 

i'm not understanding i've done wrong, lend hand, have lua experience, have started learning how use metatables, i'm new @ point, running using scite, luaforwindows. error on last line, line above runs perfectly

you have forgotten set __index field:

vector2 = {x = 0, y = 0, magnitude = 0, unit = nil} vector2.__index = vector2 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -