I have a class that inherits from AActor, and i want to inherit classes from that class, so that i can override a function. That way i can easily call that function for every subclass and it will work. But since multiple inheritance is not allowed in UE4, i don’t really know an Object oriented design that still allows calling 1 function, no matter which subclass.
First checking the type and then calling a function depending on that that is kinda bad so i’m interested in the right solution.
why don’t you need multiple inheritance for that? simply inherit your baseClass from aactor, put in your function prototypes and your childBPs inherit from your base class. in this childs you can overwrite the functions or extend it (call parent in BP or super in C++). The other thin you may want to use is an interface. It doesn’t bring you hard references and forces you to cast around all the time. just think about which functionality you need and abstract that away in interface calls.
eg (maybe I totally missunderstood you): you want an interactable “thing”. create an interface interactable, put in a function interact. implement that interface in a door to open, in a chair to trigger a animation montage, in a button to push it etc…and you can put in your playercontroller something like an overlap, check if the overlapped actor implements your interact interface and call the interact function on that actor - thats it
o ■■■■ i did not think this one through. Thought i’d have to inherit the subclasses from AActor and the baseclass but it’s just from the baseclass so it does work. Thanks though.
Allowing multiple inheritance makes the rules about function overloads and virtual dispatch decidedly more tricky, as well as the language implementation around object layouts. These impact language designers/implementors quite a bit, and raise the already high bar to get a language done, stable and adopted.
It is simple to think this way, if class A inherits from multiple classes, then the class A will have the same grandparent class multiple times, this means the code will be complicated and a series of bugs will go unacknowledged. Personally, I think multiple inheritance has a bad rap, and that a well done system of trait style composition would be really powerful/useful… but there are a lot of ways that it can be implemented badly, and a lot of reasons it’s not a good idea in a language like C++.