Start typing to search packages!
class
By @vocksel
Roblox
Mirroredclass()
A super simple module for defining Lua classes.
This was designed for use with Roblox, and as such follows their commonly used OOP conventions, but it works just fine in any Lua project.
Usage
local class = require(game.ReplicatedStorage.Class)
local Person = class("Person")
-- This method defines the parameters your class takes
function Person:__init(name, age)
self.Name = name
self.Age = age
end
function Person:Greet()
print("Hi, my names " .. self.Name .. " and I'm " .. self.Age .. " years old.")
end
-- To instantiate, you call the `new` function
local person = Person.new("John", 18)
person:Greet() -- "Hi, my names John and I'm 18 years old."
Inheritance
To make a class inherit another, you pass in the class you want to be inherited as the second argument.
The only thing you need to do manually is call the __init method of the
superclass, otherwise you won't inherit the super's properties.
The superclass is also returned after the newly created class so that you can create a reference to it. This saves you the trouble of using a direct reference to the superclass, and having to change it everywhere if you want to inherit something else.
-- This example continues from the one above
local Guy, super = class("Guy", Person)
function Guy:__init(name, age, manliness)
-- This calls Person.__init on this instance, and sets the `Name` and `Age`
-- properties for you.
super.__init(self, name, age)
-- Now we can extend it and add on our properties.
self.manliness = manliness
end
function Guy:AttackABearOrWhateverGuysDo()
if self.manliness > 50 then
print("I could totally fight that bear.")
else
print("Here lies " .. self.Name .. ". He tried to fight a bear.")
end
end
local guy = Guy.new("John", 18, math.huge)
guy:Greet() -- "Hi, my names John and I'm 18 years old."
guy:AttackABearOrWhateverGuysDo() -- "I could totally fight that bear."
If your subclass does not change anything about the parameters it takes, you
don't need to create an __init method for it. The subclass will automatically
use the __init of the superclass.
Contributing
Install Rojo and then run the following commands:
$ rojo build -o place.rbxlx
$ rojo serve
Open the newly generated place file and start the Rojo plugin.
From here you can modify anything under src/ and your changes will be synced in.
When you're ready to test, simply press F5 to play the game.
Package Details
Install command (Click to copy)
Version
0.1.0
License
MIT
Safe for commercial use
Automated license review — not legal advice.
