2
votes

I have a class with this set of values and functions:

require("class")

entity = class:new()

function entity:new()
    self.x = 100
    self.y = 100
    self.width = 32
    self.height = 32
    self.info = "entity"
    self.alive = true
    self.color = {r = 255, g = 0, b = 0}

    return self
end

function entity:load()
end

function entity:update()
    if self.alive then
    end
end

function entity:draw()
    if self.alive then
        love.graphics.setColor(self.color.r, self.color.g, self.color.b)
    end
end

function entity:destroy()
    self.alive = false
end

And I want to be able to use those same functions and values simply for other classes, like so:

require("entity")

local player = entity:new()

function player:load()
   self.color.r = 100
end

function player:update()
end
--etc etc

I come from a flash and As3 background, which if any of you know you may understand more or less what I'm trying to do. So could anyone help me with this? All help is appreciated.

1
What class library are you using? Classes aren't built into Lua. Are you sure entity:new() creates a subclass and not an entity instance (if there's a difference)? - Colonel Thirty Two

1 Answers

1
votes

You can use this class system to get the experience you are desiring to have. (Make sure to copy the code of the full version).

Your code would look like this:

require("class")

entity = class()

entity.x = 100
entity.y = 100
entity.width = 32
entity.height = 32
entity.info = "entity"
entity.alive = true
entity.color = {r = 255, g = 0, b = 0}


function entity:load()
end

function entity:update()
    if self.alive then
    end
end

function entity:draw()
    if self.alive then
        love.graphics.setColor(self.color.r, self.color.g, self.color.b)
    end
end

function entity:destroy()
    self.alive = false
end

And the second file:

require("entity")

local player = class()
player:addparent(entity)

function player:load()
   self.color.r = 100
end

function player:update()
end