Hi! Thank you very much for this tutorial, I was able to understand metatables better! Is there any way to implement function overwriting? Thanks
Hi,I know it’s been several month since you did this post but can you answer a question that has been bothering me for a while and it’s that why do people use self and why most oop are using it .
Thanks either way
Sorry for the late reply, if you haven’t already figured it out by now, self is basically a way to call the table of information you’ve created in your example.New()
function.
If you want a more detailed explanation with examples, let me know!
I apologize for the late reply (I really outdid myself with that one)
I’d like you to elaborate on what you mean by function overwriting. Like the ability to reconstruct a function and it’s ability from scratch? I’m not too sure what you mean.
If you dont mind answering another question and that is why is oop so important I mean like a table that just contain info about the gun how do people find that useful I just dont get why people use it
There is already a tutorial from 2014 about this…
though I appreciate this, I am using OOP now
You can have not only info but everything you need for your gun.
People will use this because it is a more efficient approach, that’s really it. You don’t have to use OOP at all but it makes it much easier for you as your games get more complicated. Duplicating a gun would not require making a new script, using classes you can make new guns with little effort etc
I cant seem to see any images. Is it my device problem or is it gone?
I can’t seem to see any either, I’m not sure why the images stopped appearing, I’ll try to re-upload the images, Thank you for your patience!
wait how would you use the “powerup” in this case
Why people still use OOP while we got module… 4 year I hear that, 4 year I never understand how the hell it can help…
It simplifies things and makes them easier to maintain and modify. You can go to one script where that one thing is located, rather than searching through multiple scripts to find it.
That what I call a module… It do that exactly
I just realized this topic is about OOP and not modules
What about the static method for metatable?
Static and member methods alike will be “inherited” using the setmetatable method.
I really want to do OOP, but I reject using metatables. Is this possible?
Yes, its possible. Example:
local module = {}
function module.func(self, a)
print(self.num + a)
end
function module.new()
return {
num = 0,
}
end
-- OR
local module2 = {}
function module2.new()
return {
num = 0,
func = function(self, a)
print(self.num + a)
end,
}
end
local mod = module.new()
mod.num = 1
module.func(mod, 2)
local mod2 = module2.new()
mod2.num = 1
mod2:func(2)
-- both print three
I just recommend using metatables as you dont need to understand them to do OOP, and you dont need to either define functions weirdly (the second one) or call them weirdly (the first one).
This is a very good tutorial, thanks!