Skip to content

Latest commit

History

40 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Luoop

A lightweight and flexible object-oriented library for Lua.

LicenseLua

Features

  • Simple class definition — Single class() function for all OOP needs
  • Constructors & destructorsinit and destroy lifecycle methods
  • Single inheritance — Extend classes with automatic method inheritance
  • Multiple inheritance — Inherit from multiple parent classes
  • Method overloading — Override parent methods in child classes
  • Parent method access — Call parent methods via _parentMethod()
  • Ad-hoc parent calls — Pass custom parameters to parent constructors/destructors
  • Singleton support — Built-in singleton pattern with newSingleton()
  • Instance cloning — Create new instances from existing objects with newInstance()

Installation

Copy luoop.lua to your project and require it:

require("luoop")

Quick Start

Basic Class

require("luoop")
-- Define constructorlocalfunctionnew(self, name)
self.name=nameend-- Create classPerson=class(new)
-- Add methodsfunctionPerson:greet()
return"Hello, I'm " ..self.nameendfunctionPerson:destroy()
self.name=nilend-- Usagelocaljohn=Person("John")
print(john:greet()) -- "Hello, I'm John"john:destroy()

Inheritance

require("luoop")
-- Parent classlocalfunctionnewAnimal(self, species)
self.species=speciesendAnimal=class(newAnimal)
functionAnimal:speak()
return"..."endfunctionAnimal:destroy() end-- Child classlocalfunctionnewDog(self, name)
self:_parentConstructor(Animal, "Canine") -- Call parent constructorself.name=nameendDog=class(newDog, Animal) -- Inherit from AnimalfunctionDog:speak() -- Override parent methodreturn"Woof!"endfunctionDog:introduce()
returnself.name.." the " ..self.speciesendfunctionDog:destroy()
self:_parentDestructor(Animal)
end-- Usagelocalrex=Dog("Rex")
print(rex:speak()) -- "Woof!"print(rex:introduce()) -- "Rex the Canine"rex:destroy()

Multiple Inheritance

require("luoop")
-- Parent classesSwimmer=class(function(self) self.canSwim=trueend)
functionSwimmer:swim() return"Swimming!" endfunctionSwimmer:destroy() endFlyer=class(function(self) self.canFly=trueend)
functionFlyer:fly() return"Flying!" endfunctionFlyer:destroy() end-- Child inherits from bothlocalfunctionnewDuck(self)
self:_parentConstructor(Swimmer)
self:_parentConstructor(Flyer)
endDuck=class(newDuck, Swimmer, Flyer)
functionDuck:destroy()
self:_parentDestructor(Swimmer)
self:_parentDestructor(Flyer)
end-- Usagelocaldonald=Duck()
print(donald:swim()) -- "Swimming!"print(donald:fly()) -- "Flying!"print(donald.canSwim) -- trueprint(donald.canFly) -- true

Singleton

require("luoop")
localfunctionnewConfig(self)
self.debug=falseendConfig=class(newConfig)
Config:enableSingleton()
functionConfig:destroy() end-- Usagelocalconfig1=Config.newSingleton()
localconfig2=Config.newSingleton() -- Same instance as config1config1.debug=trueprint(config2.debug) -- true (same object)Config.destroySingleton() -- Clean up

API Reference

Class Creation

FunctionDescription
class(init, ...)Create a new class with constructor init and optional parent classes

Instance Methods

MethodDescription
ClassName(...)Create new instance (calls init)
obj:destroy(...)Destructor (implement in your class)
obj:newInstance(...)Create new instance from existing object

Parent Class Methods

MethodDescription
obj:_parentConstructor(Parent, ...)Call parent's constructor with arguments
obj:_parentDestructor(Parent, ...)Call parent's destructor with arguments
obj:_parentMethod(Parent, "methodName", ...)Call parent's method
obj:_hasParentClass(Parent)Check if class inherits from Parent

Singleton Methods

MethodDescription
ClassName:enableSingleton()Enable singleton mode for class
ClassName:disableSingleton()Disable singleton mode (before instantiation)
ClassName:isSingleton()Check if class is singleton
ClassName.newSingleton(...)Get or create singleton instance
ClassName.destroySingleton(...)Destroy singleton instance

Examples

See the example/ folder for complete working examples:

Related Projects

  • Loogine — A game engine built on Luoop

License

MIT License — see LICENSE for details.

About

Easy and flexible object orientated library for Lua

Topics

Resources

Stars

11 stars

Watchers

3 watching

Forks

Releases

Packages

Contributors

Languages