What Is BaseClass?
BaseClass is a lightweight module script that helps developers organize classes in the Explorer window, automatically managing metatables. It also serves as the root for all other classes.
Basic Usage
Before using any library, you must first require()
it.
local new = require(game.ServerStorage.BaseClass)
Avoid using
require()
on individual class modules, as they will not be properly initialized.
Classes are organized under the main module, and you can access them with a simple lookup. To create an object, simply call the class you want to instantiate.
local obj = new.Animal.Dog("Rover")
The returned object is now ready for use.
Creating Classes
A class is defined by the table returned from a module script, with inheritance determined by its parent.
Here’s an example of how it would look in the Explorer window:
Most classes will have a constructor, along with various properties and methods associated with them.
Below is an example of a basic class:
local Animal = {
Name = "Animal";
}
function Animal:new(name)
self.Name = name
end
function Animal:Eat()
print(self.Name, "is eating")
end
return Animal
Extending Functionality
By inheriting from a parent class, the subclass gains access to its properties and methods, allowing you to extend or override functionality.
The following demonstrates how to access members of a class’s parent:
local super = require(script.Parent)
function Class:Method()
super.Method(self)
end
Remember that the first parameter must be
self
, and note the use of a period (.) instead of a colon (:).
If you don’t manually call a base method, its behavior will be completely overridden.
In Conslusion
You should now have enough knowledge to start creating your own classes.
Have fun and enjoy!