I want to call a method of my base class from my sub class (and in the future probably more subclasses).
basically im trying to call my base classes destroy method from my subclasses destroy method because when i call destroy on my subclass i want it to also go through the logic from the destroy method of my base class. the problem is i dont know how to access this method from within my subclass as they have the same name, so i cant just do self.Destory().
i should also add that my destroy method uses dot notation and looks kinda like this Class.Destroy(self : _Class): (). ← (this is not the actually code from my script just an example)
_Class is the type i made for my class.
i have tried just renaming the Destroy method in my base class to something like Dispose. then i would be able to call self.Dispose from my subclass’s destroy method. but this means that if i want to inherit from my subclass i would need to use a different name for that classes destroy method and same with any more inheritances down that chain that have a destroy method. this seems like a really annoying solution i want a way to be able to just call the destroy method of the parent class relative to the class im in that way even if im like 3-4 inheritances deep i can still call all of the destroy methods easily without all the methods having different names. i dont know how to word that well so if it didn’t make much sense then thats my bad.
i have also tried doing something like this getmetatable(self).__index.Destroy(self) where it calls the destroy method from the metatable of itself but this ends up with a orange underline message underlining the first self. saying something along the lines of
Type (my _Class type) could not be converted into { @metatable {- __index: (my class) -}, {- -} }
i dont really understand what this means. if i had to guess i think its because im in strict mode and the getmetatable function must have some special kind of table passed through but the self paramaters type (my class’s type) does not match with what it expects to be passed.
another thing that i have thought of was just calling the .destroy method of my base class that is passed when i require it but passing through my subclass as self rather than the base class. so maybe something like.
function SubClass.Destroy(self : _SubClass): ()
BaseClassModule.Destroy(self)
end
but with the way that im doing oop is a little strange in the way that when requiring my base class i only have access to the .new function and none of the other methods until you call the .new method witch will return an object that actually has all of the methods inside. if you dont understand what i mean im basically doing what this guy did in his post My Approach for OOP in Luau in terms of oop.
so i think i could make it so the .Destroy method is also accessible from just requiring the class or i could just do something like this instead
NPC_Class.new().Destroy(self)
so im just creating a new instance of my base class just to use the destroy method on my current subclass. while i think that both of these would work i wonder if there is any better way to call a method of a base class from a sub class. maybe im just being overly complicated and i should just use one of these solutions i listed here and if thats the case and there really is no better way to do it, then im willing to accept that and move on, but i though i would just post this on here to see if anyone else maybe had a similar situation to mine at one point and found a easy work around that keeps the clean structure of the class intact.