[Library] zClass, inheritence based class system.

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
DanielPower
Citizen
Posts: 50
Joined: Wed Apr 29, 2015 5:28 pm

[Library] zClass, inheritence based class system.

Post by DanielPower »

zClass is the OOP library I wrote for the engine I'm working on, "zokEngine". It's fairly simple, but I thought it may be of use to someone who needs to do object orientated programming in lua, and doesn't want to write their own library.

The syntax is quite different from "lclass", which I found out about just recently after writing this library. Whether this is as good as "lclass" or not, I'm not sure. I'd consider myself a novice programmer, but I prefer the workflow of my library.

It's on gitHub, along with some quick documentation I threw together in the Readme. I'll also paste an example Class here.

Code: Select all

local Kid = {}
Kid.name = "Kid"                            -- Name of the class (should be same as table name above)
Kid.parent = "Dad"                          -- Name of parent's class
function Kid.new(args)
    local self = {}                         -- Instance variables are stored in this table
    self.name = args.name or "Riley"
    self.age = args.age or 3                -- Get age from creation arguments, or default to 3
    self.favoriteColor = "green"
    self.gender = args.gender or "male"

    -- Public Functions
    -----------------------------------
    function self:create()                  -- The 'create' function is automatically called on instance creation.
        _zokClass.addList(self, "alive")        -- Adds instance to the list "alive"
        print("Hello World!")
    end

    function self:speak()
        print("My name is "..self.name)
    end

    return (self)
end
return (Kid)

Code: Select all

zClass = require "zokClass"                 -- Import library
zClass.import(classes)                      -- Import a table of class files

-- Creates instance of class 'Kid'
-- Any variables given in the argument table will override the class defaults
lesley = zClass.instCreate( zclass.classList.Kid,
                            {   name="Lesley",
                                gender="female",
                                favoriteColor="orange" })

-- Access instance variables
print(lesley.name)
print(lesley.gender)
print(lesley.age)                           -- Note that since we didn't specify an age, it will print 3, because of the default in the class file

-- Overwrite instance variables
lesley.name = "Linda"
lesley.gender = "male"
https://github.com/DanielPower/zokClass
Last edited by DanielPower on Tue Feb 16, 2016 11:26 am, edited 1 time in total.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: [Library] zClass, inheritence based class system.

Post by Robin »

I think you forgot to include a link to the repo...
Help us help you: attach a .love.
User avatar
DanielPower
Citizen
Posts: 50
Joined: Wed Apr 29, 2015 5:28 pm

Re: [Library] zClass, inheritence based class system.

Post by DanielPower »

Indeed I did. Added link to the original post.
Post Reply

Who is online

Users browsing this forum: No registered users and 203 guests