Hey forums,
can someone please explain to me what an interface is exactly, and why it is to be used in replace of multiple inheritance??
Thanks,
Jarrad T.
Hey forums,
can someone please explain to me what an interface is exactly, and why it is to be used in replace of multiple inheritance??
Thanks,
Jarrad T.
An interface, or in Unreal engine terms, a UINTERFACE() is a class definition with only pure virtual functions.
By having classes extend an interface, you guarantee that those said derived classes must implement the defined virtual functions.
This allows you in turn to Cast<YourInterface>(YourDerivedClass), i.e. , cast your class as the interface, for pointer storage in a polymorphic generic container (a container, such as a TArray<YourInterface *> which can hold YourDerivedClass which has been casted to YourInterface). However, when you call functions of the casted class as YourInterface, you call the implemented functions from YourDerivedClass.
This is a really simplistic explanation, but if you want more knowledge you should look up Pure Abstract Class in C++ (the c++ version of an Interface, of which UE4 steals the name from other famous languages ).
thanks for the answer MDSRS, but I feel like I am still missing something… how does this substitute multiple inheritance…
Using interfaces in C++ is using multiple inheritance. Other languages like Java would force you to inherit from one concrete class and all the others must be interfaces. In C++ you don’t face any such language imposed restriction but you can choose to adhere to that rule.
With UCLASS() objects from UE4 you pretty much have to adhere to that rule because C++ style laissez faire multiple inheritance will not work with these objects.
Answer: it does not substitute multiple inheritance.
Example: “InteractiveObject” interface which can be either an Actor, SkeletalMesh, or StaticMesh that the player can click to interact with.
Example: a “SimulationObject” interface which communicates with a separate simulation via a messaging system. It can be a Character, an Actor, or a Components.
In both cases a “class” that parents or extends all 3 not only doesn’t work but also makes zero sense. Advise you to do some interface tutorials because you will use them a lot!
An interface, as a programming concept, is a “contract” that guarantees the type implementing the interface implements all the methods the interface declares. For code consuming the interface, the type and it’s genealogical tree doesn’t matter as long as it implements the interface.
C++ does not have a native language construct for interfaces, so they are traditionally implemented using multiple inheritance with abstract virtual methods. An interface does not declare any new data fields and thus does not modify the data layout of the types that implement it.
Thanks for the answer guys, been having a look at a handful of tutorials now… powerful stuff.
It’s quite rare that you need to use them in Unreal to be honest, as most of the time this is what the Component system replaces. Multiple Inheritance is UE4 is tricky at times, because you can only inherit from one UObject-type at a time.
If for example, you have both Characters (APawn->ACharacter) and Vehicles (APawn->AVehicle) in your game and you want them to have a unified “health” system, you can’t add that system to the native ‘APawn’ class without creating a custom engine build. To get around it, you can either create a Health Interface which they both inherit from, or you can create a Health Component.
Components tend to be much easier to work with so I would suggest using them wherever possible in place of Interfaces, especially if your project is a mixture of C++/Blueprint which nearly all projects are.
Casting to interfaces starts to become ugly after a while, especially where blueprint is involved. C+±only interfaces are much easier to work with.
To build on TheJamsh answer, or maybe get corrected, it seems that the only real need for an interface would be as a component provider. As in, you use an interface to be able to attempt to fetch identical components (i.e ,an health component) from multiple UObject types.
essentially what I am after is heading advice from a fellow developer who stated the following:
“If you want a vehicle to be interactable as well as other stuff, you’re going to want to make an Interface called Interactable. This lets you have any class extend from whatever it wants and also extend from Interactable. See this: wiki.unrealengine.com/Interfaces_in_C%2B%2B”
because I want my character to be able to interact with interactable objects, wether that be weapon pickups, ammo pickups, interacting with doors and Unreal’s vehicle system.
The problem I am currently facing is the vehicle aspect, as I want to make myOwnVehicle c++ class that doubles up as an unreal vehicle and an interactable object (in which I have implemented).
That’s fine, the Vehicle can inherit the base vehicle class from Unreal and the interactable interface at the same time.
Example:
UCLASS()
class MYGAME_API AMyVehicle : public AWheeledVehicle, public IMyInterface
{
}
http://www.reactiongifs.com/r/msty.gif
I use Interfaces only when necessary (For example all my AnimBPs implement an Interface, so the Pawn can easily call necessary functions on it), but the usual case is using Components + Event Dispatchers.
heyo everyone,
thanks for the replies will work on it and see what I come up with, thanks again
Jarrad T.