Not a member yet? Why not Sign up today
Create an account  

  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 
setmetatable?

#1
here is code
Code:
MyClass = {} -- the table representing the class, which will double as the metatable for the instances
MyClass.__index = MyClass -- failed table lookups on the instances should fallback to the class table, to get methods

-- syntax equivalent to "MyClass.new = function..."
function MyClass.new(init)
  local self = setmetatable({}, MyClass)
  self.value = init
  return self
end

function MyClass.set_value(self, newval)
  self.value = newval
end

function MyClass.get_value(self)
  return self.value
end



function Update(I)
local tab = MyClass.new(5)
-- tbl:name(arg) is a shortcut for tbl.name(tbl, arg), except tbl is evaluated only once
print(tab:get_value()) --> 5
tab:set_value(6)
print(tab:get_value()) --> 6
end
and error message
Code:
[string "MyClass = {} -- the table representing the cl..."]:6: attempt to call global 'setmetatable' (a nil value)
stack traceback:
    [string "MyClass = {} -- the table representing the cl..."]:6: in function 'new'
    [string "MyClass = {} -- the table representing the cl..."]:22: in function 'Update'
    [string "temp buffer"]:74: in function 'RunUpdate'
    [string "temp buffer"]:80: in function <[string "temp buffer"]:78>
I was following this tutorial LINK
I want to use object orientation to make PIDs for my plane
Reply



Messages In This Thread
setmetatable? - by Fernir - 2018-06-27, 11:14 AM
RE: setmetatable? - by khaz - 2018-06-27, 12:39 PM
RE: setmetatable? - by Fernir - 2018-06-27, 02:19 PM

Forum Jump:


Users browsing this thread:
1 Guest(s)