Page 2 of 2

Re: multiple inheritance?

Posted: Sat Jun 04, 2016 8:24 am
by airstruck
Skeletonxf wrote:I've ran into a problem where trying to make Box composed of a point seems to over write its meta table.
Your "new" functions allow you to pass in a table to use as the instance. You pass the same "args" table to both Box:new and Point:new, so your box and your point are both the same table. When you pass that table to Point:new, it replaces the metatable set by Box:new.

Re: multiple inheritance?

Posted: Sat Jun 04, 2016 10:10 am
by Skeletonxf
Oooh, that makes sense. Ha ha my next planned step in passing through just the x and y values to point fixes it xD

Code: Select all


--wrapper for public access to creating Boxes
function box.new(args)
  local pointArgs = {}
  if args["x"] then
    pointArgs["x"] = args["x"]
    args["x"] = nil
  end
  if args["y"] then
    pointArgs["y"] = args["y"]
    args["y"] = nil
  end
  local o = Box:new(args)
  -- box is composed of a point
  print("making box")
  print(inspect.inspect(o))
  o:givePoint(pointArgs)
  return o
end

Code: Select all

making box
{
  h = 50,
  message = "ok",
  w = 50,
  <metatable> = <1>{
    __index = <table 1>,
    draw = <function 1>,
    getArea = <function 2>,
    getCentreX = <function 3>,
    getCentreY = <function 4>,
    getH = <function 5>,
    getW = <function 6>,
    getX2 = <function 7>,
    getY2 = <function 8>,
    givePoint = <function 9>,
    h = 50,
    isMouseOver = <function 10>,
    name = "box",
    setCentre = <function 11>,
    setCentreX = <function 12>,
    setCentreY = <function 13>,
    setH = <function 14>,
    setW = <function 15>,
    w = 50,
    <metatable> = <2>{
      __index = <table 2>,
      name = "",
      new = <function 16>
    }
  }
}
giving box ok a point
{
  h = 50,
  message = "ok",
  point = {
    x = 10,
    y = 50,
    <metatable> = <1>{
      __index = <table 1>,
      getMessage = <function 1>,
      getX = <function 2>,
      getY = <function 3>,
      message = "",
      moveX = <function 4>,
      moveY = <function 5>,
      name = "point",
      new = <function 6>,
      setMessage = <function 7>,
      setX = <function 8>,
      setXY = <function 9>,
      setY = <function 10>,
      x = 0,
      y = 0,
      <metatable> = <2>{
        __index = <table 2>,
        name = "",
        new = <function 11>
      }
    }
  },
  w = 50,
  <metatable> = <3>{
    __index = <table 3>,
    draw = <function 12>,
    getArea = <function 13>,
    getCentreX = <function 14>,
    getCentreY = <function 15>,
    getH = <function 16>,
    getW = <function 17>,
    getX2 = <function 18>,
    getY2 = <function 19>,
    givePoint = <function 20>,
    h = 50,
    isMouseOver = <function 21>,
    name = "box",
    setCentre = <function 22>,
    setCentreX = <function 23>,
    setCentreY = <function 24>,
    setH = <function 25>,
    setW = <function 26>,
    w = 50,
    <metatable> = <table 2>
  }
}

Now I suppose I just need wrapper methods in Box for operating on the instance of point

Edit: for any onlookers viewing this thread in the future you can see the changes from inheritance I had to composition now here https://github.com/Skeletonxf/menu-syst ... a99d129e61.