davisdude/mlib

Objects created with MLib.Shape.NewShape cannot have metatables

Closed this issue · 3 comments

Skip this:

This does not work:

local Table = {}
Table.__index = Table

function Table.New( x, y, Radius )
    return MLib.Shape.NewShape( x, y, Radius )
end

function Table.Draw( Self )
    love.grapihcs.circle( 'fill', Self.x, Self.y, Self.Radius )
end

function love.load()
    Thing = Table.New( 4, 4, 4 )
end

function love.update( dt )
end

function love.draw()
    Thing:Draw()
end

A work-around would be to store the information for collision as a variable, such as (as an addendum to the above code):

function Table.New( x, y, Radius )
    local Collision = MLib.Shape.NewShape( x, y, Radius )
    return { x = x, y = y, Radius = Radius, Collision = Collision }
end

Read this:

One solution to this problem is this:

local MLib = require 'Path.To.MLib'
local Ent = {}
local Shape = MLib.Shape.NewShape( 1, 1, 1, 1, 1, 1 )
local OldMeta = getmetatable( Shape ) --
Store the metatable
Ent.__index = OldMeta
MLib.Shape.Remove( Shape.Index ) -- This keeps false collisions from happening

function Ent.New( x, y )
    local New = { x = x, y = y }
    New.__index = Ent
    setmetatable( New, Ent )
    return New
end

-- etc.

What this does is it sets the reference to Ent first, then to MLib. This
way, so long as you don't have a function called "Ent.Remove", "Ent.New",
etc. it goes straight to MLib's.

Thanks for the swift solution post! I'll see if I can't utilize it to solve my polygon collision.

No problem! Hope it helps.
Check out the edit to the main post- I fixed a problem that would be caused by making objects prior to loading that.

Sounds great thanks for the update.