- What are classes?
- Creating a new class
- Instantiation
- Constructors
- Variables & Functions
- Destructors & Other Metatable Magic
Classes are simply a way to organize data to reflect an object. For example, you might have a player class that represents the player's object in the game world. That class would contain things such as health, position, status, and whatever else you can think of.
Classes can also use inheritance. Inheritance means that one object shares certain functionality with its "parent." If you were to make an object to represent a cat or dog, you might inherit behavior from the animal class.
To create a new class, all you need to do is call class.new(). Calling that function with no parameters will return an empty class and will not call its constructor. If you call class.new() and pass it an object, it will return a new class that inherits behavior from that object.
Declaring a new class does not give immediately give you an object that you should use directly. Instead, think of it like a recipe: it tells you what the object contains, but it isn't ready to use.
In order to actually create an instance of a class, you need only to call its baseclass as a function, like so:
Constructors are the functions that get called when creating an instance of a class. If we wanted our Dog to bark when we create one, we could do this:
In order to actually create an instance of a class, you need only to call its baseclass as a function, like so:
A constructor can also take arguments. If, for some strange reason, you wanted to pass in the dog's age when creating it, you can do this:
You may also wish to call its parent's constructor. You must be careful here. Do not call self.parent:constructor() directly, as doing so may cause an infinite loop and possibly a stack overflow. The proper way to do it is to specify the parent by calling classname.parent.constructor(self) instead.
Before Dog constructs itself, it might want to first initiate itself with some data:
A class can contain any number of variables. It is recommended that class variables are declared in its constructor for clarity and for inheritance reasons. When calling from inside of a class, variables and functions, should be preceeded by "self." and when called from outside you should use "." to access variables or ":" to call functions.
You may override a class's metatable in order to change its behavior. You might want to override __gc to be a destructor: a function that is called when the object is deleted. You may also want to allow the object to be used with operators such as __add (+), or __sub (-). In order to do this, you'll need to modify the metatable associated with that operation.
Page last updated at 2018-09-25 20:47:17