Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR LUA

lua class

MyClass = {foo = "Hello", x = 0} -- Default values
MyClass.__index = MyClass

function MyClass.new(arg)
    local object = {}
    setmetatable(object, SomeClass)
  
  	object.arg = arg
  
    return object
end

function MyClass:bar() -- Be careful, not a '.' but a ':'
  print("World !")
end

function MyClass:addOne()
  self.x = self.x + 1
end

obj1 = MyClass.new("a")
obj2 = MyClass.new("b")

print(obj1.foo) -- Print : Hello
obj2.bar() -- Print : World !

print(ob1.x) -- Print : 0
print(ob2.x) -- Print : 0
obj2:addOne()
print(ob1.x) -- Print : 0
print(ob2.x) -- Print : 1

print(obj1.arg) -- Print : a
print(obj2.arg) -- Print : b
 
PREVIOUS NEXT
Tagged: #lua #class
ADD COMMENT
Topic
Name
1+4 =