Gagan
Gagan

Reputation: 5656

Lua inserting a record to a table is failing

I have a main script as follows . This script is called script.lua (for the sake of this example)

require "modules\\myparentclass"
require "modules\\condition"
require "modules\\helpers"
require "constants"

parentclass = MyParentClass:new()
print ("MyParentClass Type : " .. parentclass:getCode())

-- add conditions

-- condition 1
condition1 = Condition:new(nil,"Are you happy?" , "YES")
parentclass:addCondition(condition1)

-- -- condition 2
condition2 = Condition:new(nil,"Are you sad?" , "NO")
parentclass:addCondition(condition2)


local l = parentclass:getConditions()

print(l[2]:getQuestion())

I have another class called MyParentClass whose code is as follows

require "constants"
require "modules\\condition"
require "modules\\helpers"

-- Meta class
MyParentClass = {code = ""}


function MyParentClass:new (o)
   o = o or {}
   setmetatable(o, self)
   self.__index = self
   self.condition = condition
   self.conditions = {}
   return o
end

function MyParentClass:getCode ()
   return "Parent Class"
end

function MyParentClass:addCondition(condition)
    print(condition)
    table.insert(self.conditions,condition)
    print('record inserted')
    -- self.conditions = {next= self.conditions, value = condition}
end

function MyParentClass:getConditions()
    return self.conditions
end

and I have a third class , Conditions which is as follows

require "constants"


-- Meta class
Condition = {question="", answer=""}

function Condition:new (o, question,answer)
   o = o or {}
   setmetatable(o, self)
   self.__index = self
   self.question = question or nil 
   self.answer = answer or nil
   return o
end


function Condition:getCode ()
   return CONDITION_TYPE
end

function Condition:getQuestion()
    return self.question
end

function Condition:getAnswer() 
    return self.answer
end

The idea is that in the main script (script.lua) ,

  1. I can create a new parent class.
  2. Each parent class can have multiple conditions (aka questions).

For me, the first part is working. However I am failing with the second part. Whenever I run the script , I get two instances of the second question. Please see the below snapshot for more details.

enter image description here.

Ideally, I would like to have both the conditions displayed ("are you happy?" and "are you sad?") but that is currently not the case.

Can you please help me out with this one?

Upvotes: 1

Views: 108

Answers (1)

Piglet
Piglet

Reputation: 28940

Take a look at your constructor

function Condition:new (o, question,answer)
   o = o or {}
   setmetatable(o, self)
   self.__index = self
   self.question = question or nil 
   self.answer = answer or nil
   return o
end

Everytime you call this you change the value of Condition.question and Condition.answer as self refers to Condition, not to your new object o!

So what you actually do is create 2 new tables condition1 and condition2 which both don't have their own .answer and .question. Therefor you access their metatable Condition which after you created condition2 contains: "Are you sad?" , "NO")

If you want to initialize members in your constructor you have to use o.answer and o.question.

Make sure you understand how that metatable stuff actually works.

Upvotes: 1

Related Questions