Shadowblitz16
Shadowblitz16

Reputation: 145

whats the difference between class and instance?

whats the difference between class and instance in lua?

I know classes are like the template and the instance is the object created from the template but I am wondering what the difference in code is.

my goal is to make a system that works like this..

--class.widget.lua----------------------------------------------------------

local class  = require "class"
local widget = class("class.widget")

function widget:create()
    --create variables here
end

function widget:update() 
    --update variables here
end

function widget:render()
    --widget render
end

return widget

--class.widget.button.lua---------------------------------------------------

local class  = require "class"
local widget = require "class.widget"
local button = class("button", widget)

function button:create(x, y)
    base:create()
    --create variables here
end

function button:update() 
    base:update()
    --update variables here
end

function button:render()
    base:render()
    --widget render
end

return button

--main.lua------------------------------------------------------------------

local button = require "class.widget.button.lua"
button1      = button(0, 0)
button2      = button(0, 16)

Upvotes: 1

Views: 244

Answers (1)

Shadowblitz16
Shadowblitz16

Reputation: 145

even though this hasn't been answered, this right here is working exactly the way I want it to

I am posting it here if anybody wants to use it

EDIT: this is a better version for both me and any one looking for a good lua class

return function(name, base)
    local class = {}

    --private vars
    class.__name        = name 
    class.__base        = base
    class.__index       = class
    class.__event       = "create"

    --events
    class.onCreate      = function(inst) end
    class.onUpdate      = function(inst) end
    class.onRender      = function(inst) end
    class.onDelete      = function(inst) end

    --functions
    class.create        = function(inst) inst.__event = "create" inst:onCreate() end
    class.update        = function(inst) inst.__event = "update" inst:onUpdate() end
    class.render        = function(inst) inst.__event = "render" inst:onRender() end
    class.delete        = function(inst) inst.__event = "delete" inst:onDelete() end
    class.getBase       = function(inst) return inst.__base end
    class.getName       = function(inst) return inst.__name end
    class.inheritEvent  = function(inst)
        if inst.__event == "create" then inst.__base:create() end
        if inst.__event == "update" then inst.__base:update() end
        if inst.__event == "render" then inst.__base:render() end
        if inst.__event == "delete" then inst.__base:delete() end
      end

    --constructor
    local    MT  = {}
             MT.__index = base
    function MT:__call(_, ...)
        local    inst = setmetatable({}, class)
        inst:create(inst, ...)
        return inst
    end

    return setmetatable(class, MT)
 end

Upvotes: 1

Related Questions