Home > database >  Lua OOP for Event Handling
Lua OOP for Event Handling

Time:11-23

I'm referencing this tutorial online: https://www.tutorialspoint.com/lua/lua_object_oriented.htm

Specifically I want a class that holds four tables. The purpose of the class is to be an EventObject. The first table will hold a reference to a function in another class, the second will hold a list of all the variables/arguments, the third will have an optional function call and string to output to the log, and the last table holds a conditional that the Event System must pass before it will do the call on the function specified in the first table.

My issue is that because I'm passing in four tables and the example only has one table and then some number variables, I'm not sure if this is the right approach and I'm struggling with getting it to do what I want.

Here's my setup code. Can anyone please tell me if there is a glaring error I've missed? This is my first time ever using metatables and metamethods and stuff so I'm a little out of my depth.

TEventObject = { method = {}, args = {}, logStatement = {}, conditionals = {} }


function TEventObject:new ( method, args, logStatement, conditionals )

  method       = method       or {}
  args         = args         or {}
  logStatement = logStatement or {}
  conditionals = conditionals or {}
  
  setmetatable(method      , self)
  setmetatable(args        , self)
  setmetatable(logStatement, self)
  setmetatable(conditionals, self)
  
  self.__index      = self
  self.method       = method or {}
  self.args         = method or {}
  self.logStatement = method or {}
  self.conditionals = method or {}

  return method, args, logStatement, conditionals
  
end

CodePudding user response:

The code in linked tutorial is incorrect.

-- Meta class
Rectangle = {area = 0, length = 0, breadth = 0}

-- Derived class method new

function Rectangle:new (o,length,breadth)
   o = o or {}
   setmetatable(o, self)
   self.__index = self
   self.length = length or 0
   self.breadth = breadth or 0
   self.area = length*breadth;
   return o
end

Should actually be

-- Meta class
Rectangle = {area = 0, length = 0, breadth = 0}

-- Derived class method new

function Rectangle:new (o,length,breadth)
   o = o or {}
   setmetatable(o, self)
   self.__index = self
   o.length = length or 0
   o.breadth = breadth or 0
   o.area = length*breadth;
   return o
end

I personally would prefer

function Rectangle:new (length,breadth)
       local o = {}
       setmetatable(o, self)
       self.__index = self
       o.length = length or 0
       o.breadth = breadth or 0
       o.area = length*breadth;
       return o
    end

As you would otherwise change breadth, length and area for all rectangles to the value assigned to the latest rectangle.

In order to figure this out you both need to know what that code does and what it shoul actually do. I know this is hard for a beginner. But the way you changed the code to your needs clearly shows that you did not even try to find out what the original code actually does. Otherwise I cannot explain how you came up with those lines.

Please refer to the Lua Reference Manual for every line of code you read until you know for sure what it does.

CodePudding user response:

I've solved it, and my solution is working.

  • Related