Still have questions? Join our Discord server and get real time help.
Log in to vote

How to properly use metatables as classes?

Asked by 5 months ago
Edited 5 months ago

So recently I've been using metatables to behave as classes pretty frequently. I read, however, that whenever a table is indexed in lua, it checks to see if the index is nil in the original table first and then references the metatable. Does this mean that it would be more efficient to create a new set of functions for each 'subclass' and abandon metatables since I do not need to update class functions dynamically? Or would that be a waste of memory?

Here's a better demonstration of what I mean to say. Which of these methods is more efficient for making classes which do not need to share data or dynamically change?


local cas = game:GetService("ContextActionService") local Action = {} Action.__index = Action function Action:new(name) --class constructor local action = {} = name return setmetatable(action, self) --Each subclass has table Action as a metatable and shares its functions end function Action:Bind() --example function cas:BindAction(, self.Handler, false, self.binding) end

Constructor Function:

local cas = game:GetService("ContextActionService") local function newAction(name) --class constructor local action = {} = name function action:Bind() --example function, each class has its own copy of the function cas:BindAction(name, self.Handler, false, self.binding) end return action end

My guess is that metatables are only useful for class structures when the subclasses have to communicate with each other, and so I should use the second method when possible to maximize read time.

Unless this is being used in code which needs to be as performant as possible (like when something is slow), you shouldn't worry about small optimizations like this. hiimgoodpack 2009 — 5mo
I'm currently working on user input so I want the read time to be as fast possible. I'm also using class structures for most of the functionality in my game so I'd like to know best use cases aquathorn321 821 — 5mo
like hiim said, you'll be fine writing the function outside of the constructor. you won't see much of a performance difference at all. being able to read the code is much more important p0etic 5049 — 5mo
Thanks for the feedback. My goal is really just to have responsive input and to mitigate server traffic so that I don't have issues when I'm running servers with 30+ people. I really have no experience with running a game yet so I'm not sure how efficient I need to be. aquathorn321 821 — 5mo
View all comments (5 more)
Ideally I won't have to retroactively change how these systems function. I'm not sure whether it's better to reduce memory use or reduce traffic at a larger scale. aquathorn321 821 — 5mo
ok p0etic 5049 — 5mo
By the way you shouldn't use : for constructors or library functions because they are not intended to be used as methods programmerHere 372 — 5mo
Dude why are you always trying to moderate my threads lol aquathorn321 821 — 5mo
And wdym by the colons not being intended for use as methods? I literally learned about using them for classes from aquathorn321 821 — 5mo

Answer this question