Jump to content
  • entries
    5
  • comments
    18
  • views
    10,924

Lua Classes Module


Averice

5,644 views

 Share

So I've got a few of these little helper modules I've written while making my game, I figured I needed a place to post them so others can get some use from it, I'll be selective with what I release as some of them I would like to keep private but everytime I get a spare moment I'll try to write up a blog post with a new module release and how it's used.

 

So anyone familiar with OOP will hopefully get some use from this module, Lua already has a semi-OOP system with it's 'metatables' and this module just extends those to be used in a class system with inheritance.

 

-- This is the definition of the system, only a small file but it does a lot.

class.lua

-- Class system written by Averice
_shardClass = {}
_shardClass.__index = _shardClass;

function class(nme)
if( _G[nme] ) then
print("Class: "..nme.." already exists.");
return;
end
_G[nme] = {}
setmetatable(_G[nme], _shardClass)
_G[nme].istype = nme;
return _G[nme];
end

-- Really no reason for the shardClass metatable but it provides nice looking functionality
-- class "CAnimatedEntity" : extends "CBaseEntity" <-- is now correct syntax
function _shardClass:extends(superclass)
if( _G[superclass] ) then
self.parent = superclass;
self.__index = _G[superclass];
end
end

function new(class)
if( _G[class] ) then
local newClass = {}
setmetatable(newClass, _G[class]);
for k, v in pairs(_G[class]) do
newClass[k] = v; -- For some reason metamethods weren't being accessed properly, here's a hacky fix.
end
if( newClass.Init ) then
newClass:Init();
end
return newClass;
end
print("Class: "..class.." does not exist.");
end

function ctype(class)
return class.istype or type(class);
end

 

Alright, if your hoping to use this and know OOP programming the above should be self-explanatory.

Simple put to define a new class you do this.

class "MyClass";

To define a class that inherits from another you do this.

class "MySecondClass" : extends "MyClass";

To create a new instance of a class you do this.

my_class_instance = new "MySecondClass";

 

If there is an 'Init()' function it will be called automatically when a new instance of the class is created.

 

Here is an example.

class "ExampleClass";

function ExampleClass:Init()
self.Number = 10;
end

function ExampleClass:SimpleFunction()
print(self.Number);
end

class "NewExampleClass" : extends "ExampleClass";

function NewExampleClass:Init()
self.Number = 15;
end

-- Now we create an instance.
MyClass = new "NewExampleClass";
MyClass:SimpleFunction() -- notice this function was inherited from "ExampleClass" not "NewExampleClass"
-- will print 15;

  • Upvote 8
 Share

5 Comments


Recommended Comments

Well that's certainly a new way to do that smile.png.

 

I didn't know lua didn't require ()'s when calling functions and then returning a table and calling a function on that without needing ()'s is a pretty neat trick to make it look like a normal OO language.

 

Would be cool if you didn't have to pass the class name as a string but could as a variable that would be nil (obviously) but could some how get the sending variable name and use that.

 

While the underlying way of doing this is fairly messy and not as neat as the method Josh uses in some of this scripts, the usage is much closer to other OO languages so I love that.

 

Also, when you instantiate the class you shouldn't have to refer to it as a string right? Because you create a table from the string when you define the class so you can have a much more OO way by just using the table directly at that point like:

 

myInstance = new MyClass

 

 

With that I guess an alternative approach to using string class names when defining your class could be something like

 

class Person = {}

 

class NPC = {} : extends Person

Link to comment

Passing them as a string is needed as it manually grabs the class from the global environment. It doesn't need to be that way but I did it for uniformity because the class () function does require string names and theres no getting around that.

Link to comment

Very nice work Averice. Especially having inheritance functionality when working with classes is something I really like to see. I am currently not using that at all in my scripts, which makes this so cool. Thanks for sharing.

  • Upvote 1
Link to comment

This is an awesome script. Is there a way to make all the classes local to the lua file instead of maintaining a global list of classes?

Link to comment

You could replace all _G mentions with a local table, although that would be quite messy and not make much sense?

 

If you could explain to me what it is you're trying to do with it I might be able to come up with a neater solution.

Link to comment
Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...