Advertisement
Guest User

Class.Gamedroit.lua

a guest
Sep 8th, 2021
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.50 KB | None | 0 0
  1. -- Gamedroit.lua - 08/09/2021 - 21:32
  2.  
  3. -- Since there is no built-in, let's use a function to create new classes
  4. -- Parameter n: Function(instance : table)
  5.  
  6. function Class(n)
  7.     -- Returns another function that will serve as a constructor, the basis for initializing a new instance.
  8.     -- Parameter ...: table - vararg to receive all parameters
  9.    
  10.     return function(...)
  11.         -- The table that will store all class data, including methods.
  12.         local data = {}
  13.        
  14.         -- Here we create a Metatable that will generate the expected behavior of a class system
  15.         local instance = setmetatable({}, {
  16.             -- Metamethod fired every time a new variable is added to the table.
  17.            
  18.             -- Note that as its first argument, setmetatable takes an empty table,
  19.             -- so every time the programmer changes the value of a variable this function will be called
  20.             -- because the values are being written to "data" table.
  21.            
  22.             __newindex = function(_, index, value)
  23.                 -- Variables with the "__" prefix are defined as private variables, only accessible inside classes,
  24.                 -- the expression below takes care of checking if the variable is private or not,
  25.                 -- besides checking if it is being accessed inside the class or outside.
  26.                
  27.                 -- Note: main refers to scope outside the class.
  28.                
  29.                 if (index:sub(1, 2) == "__" and debug.getinfo(2).what == "main") then
  30.                     return nil
  31.                 end
  32.                
  33.                 -- If everything is ok, save changes in data table.
  34.                 data[index] = value
  35.             end,
  36.            
  37.             -- Metamethod fired every time a table index is accessed.
  38.            
  39.             __index = function(_, index)
  40.                 if (index:sub(1, 2) == "__" and debug.getinfo(2).what == "main") then
  41.                     return nil
  42.                 end
  43.                
  44.                 -- If everything is ok, returns the index value within the data table.
  45.                 return data[index]
  46.             end
  47.         })
  48.        
  49.         -- Call our constructor to define our instance's default variables.
  50.         n(instance)
  51.        
  52.         -- Fires the "main" function with the arguments passed to the constructor.
  53.         instance:main(...)
  54.        
  55.         -- returns the instance of the class.
  56.         return instance
  57.     end
  58. end
  59.  
  60. -- Here a simple test, we create the Person class that defines a global variable name
  61. -- and a private variable __createdAt that refers to the creation date.
  62.  
  63. local Person = Class (function(self)
  64.     self.__createdAt = os.time()
  65.     self.name = ""
  66.    
  67.     -- The main function, which will always be called at the beginning of a new instance,
  68.     -- which receives the parameter "name".
  69.     function self:main(name)
  70.         -- Sets the value of name on instance to the value of the parameter.
  71.         self.name = name
  72.     end
  73.    
  74.     -- A function that serves to return the value of the private variable __createdAt
  75.     function self:getCreatedAt()
  76.         return self.__createdAt
  77.     end
  78.    
  79.     function self:setCreatedAt(value)
  80.         self.__createdAt = value
  81.     end
  82. end)
  83.  
  84. -- Instance the Person class with the name "Andrew".
  85. local person1 = Person("Andrew")
  86.  
  87. -- Attempts to access the __createdAt private variable from outside the class
  88. -- and tries to set a value to it. It doesn't return an error but it doesn't make any changes.
  89. person1.__createdAt = 12
  90.  
  91. -- Prints the person's name -> 'Andrew'.
  92. print('name:', person1.name)
  93.  
  94. -- Trying to access the value of the __createdAt variable directly. -> 'nil'
  95. print('direct:', person1.__createdAt)
  96.  
  97. -- Trying to use getter method for __createdAt. -> 'time : number'
  98. print('getter:', person1:getCreatedAt())
  99.  
  100. -- Trying to use the setter method for __createdAt
  101. person1:setCreatedAt(100)
  102.  
  103. -- getter method for __createdAt. -> '100'
  104. print('getter:', person1:getCreatedAt())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement