Thats so epic! More people should use this method as its extremely easy to use
Yeah they really should I literally made a mission system out of it and its very good, you can use this for literally anything good luck!
Thank you so much for your kind words. I’m gonna come back to you because you are legendary
That certainly is an interesting way of doing OOP as your variables are genuinely private (not just “don’t touch these pls” private). Lua is a very flexible language with no single best way to do OOP so it’s always good to experiment, but it’s also important to understand the drawbacks to certain ways of doing things.
Firstly inheritance won’t work the way you seem to expect. In your character class you define your variables in a local scope (well actually you don’t but you need to, you can only have one character object if your variables are global like that) which means they can only be accessed by the functions you define in that scope. Ie the functions of your character object. The rogueclass functions cannot access these variables and so rogue.DealDamage
does not subtract from the character’s health, it’s actually subtracting from the value you passed to the constructor (try rogueclass.new():DealDamage(10)
).
Secondly you redefine all your functions for every object you make. This incurs extra memory and processing overhead especially with how you store your variables as upvalues (in a local scope) rather than in a table. I’m not sure how much of an impact this actually has, probably relatively small, but it’s worth keeping in mind. Incapaz mentioned this when he said “every time you call your constructor you create a new :DealDamage
method.”
Saying that, having genuinely private variables is really cool and if your use case requires them then this could be the best method.
Yeah not alot of people use my method and im glad I got someone to use it, its really nice and just the most basic version of oop, metatables are really not needed they just make things more, structured I guess? Well anyway thanks for replying!
Also about the rogue class, that was just experimenting with the deal damage thing, also I dont redefine functions, the rogue class inherits the functions GetHealth and GetSpeed from the character class I made
You may only write the functions once but they get created again for every object you make.
Using the normal oop method does the exact same thing
Yeah it’s a really cool method. I already know that I can use
rogueclass.new():DealDamage(10))
in modern lua. However, this method in my opinion is also nice because im not getting my hand stuck in metatables. I’ve been learning it for a month and i didn’t learn much, so you can imagine how hard it is for me to learn it. However, using @loveicicIe’s method is like a cheat sheet for me. It’s a nice alternative to metatables. But, I do like @magnalite’s idea a ton.
However, the biggest problem is this.
what do i do with the excess code and how to I call it outside of the constructor
Now, when I try @magnalite’s method, I think we can’t edit it in the specific timeframes we want
-- what I tried to do the whole time was this
local rogue = rogueclass.new(100,50,16, "Fire").TakeDamage(25)
-- This method works perfectly if I were to use metatables
-- then we can't edit this on specific time frames
wait(1)
rogue.TakeDamage(25)
the reason why my script broke to begin with was because I put the function inside of the constructor.
also I don’t like to use “self” when doing
rogueclass.takedamage = function(self, initaldamage)
if self.health < initaldamage then
self.health = 0
else
self.health = self.health - initaldamage
end
end
No, the method using metatables stores only one copy of each function in the metatable which each object can then redirect to. This way we only store data about our object in our object and all the functions are in one table. This is different to your method where you store variables as upvalues and your object is a table of functions which have access to those upvalues. (This is also why using self won’t reference your object variables as they aren’t stored in the object table)
Well it works either way, thanks for your contribution to this post!