Classes

A class is the template or blueprint that object instances are built from. A class defines variables and functions that every object made from that class is guaranteed to have. Each object, however, has its own copy of these variables, independent of each other.

For example, if we have a class to represent the enemies in a game, we can assume that each enemy has some health, an attack value, and some defense value. This is pretty easy to code up:

Enemy = { }Enemy.health = 200Enemy.attack = 4Enemy.defense = 20

This Enemy table will serve as the blueprint for all enemy objects. So, how can you create new objects from this blueprint? In OOP terms, a constructor is needed. A constructor is a function that instantiates a new object from ...

Get Lua Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.