I have a general questions about Interfaces. When reading through the documentation and epics wiki I see that a call to the interface should be by using Execute_Prefix.
Since I am not getting it to work with Execute_Prefix I just went head and trialed and errored a bit until i implemented it in this way:
Now it works it calls the right interface and I see why but for some reason I feel a bit unsure if I still should try to get it to work wit Execute_Prefix.
You are sure you properly declared interface with UINTERFACE(), UIdentityActorComponentDataInterface class and GENERATED_BODY() on both I and U class? Considering Execute_ don’t work and you don’t get assert fail on execution of normal function, means there might be problem in decleration.
Now what you do will indeed work, it still 100% correct in C++ (in fact it is not interface, C++ simply supports multi-parent classes, which can work like interface, UE4 does not support that with UObjects), but you compliantly bypassing enigne management over it, engine don’t see that interface and don’t see being any functions being called. Reason behind Execute_ function is to make interface work in blueprint, if you gonna directly call interface function in C++ the call won’t be forward up to blueprint system, it will only execute code in C++. So what you doing will work in C++, but won’t work in blueprint, there also potential of some errors by using interface like that, because again what you have is caused by UE4 not adding this interface in reflection system so engine have 0 idea of this interface and functions existence.
Remember that regardless of people calling it “UE4 C++” it is still normal C++ and all C++ code incompatible with UE4 APIs will build and work, but if you not going to be careful with it you will get errors and crashes.
First off thanks a lot for your extensive answer. That makes a lot of sense. And as you said in the last sentence I would like to go the careful route to implement as intended by the UE4 API.
I am very sure I did declare it. Here you go:
UINTERFACE(MinimalAPI)
class UIdentityActorComponentDataInterface : public UInterface
{
GENERATED_BODY()
};
class IIdentityActorComponentDataInterface
{
GENERATED_BODY()
/************************************************************************/
/* Defaults */
/************************************************************************/
public:
/**
* Initializes and loads all data from the Component
*/
virtual void InitializeActorComponent(TSubclassOf<UIdentityParadigmComponent> ParadigmComponent);
virtual void InitializeActorComponent(TSubclassOf<UIdentityWeaponComponent> WeaponComponent);
virtual void InitializeActorComponent();
};
The solution is basically message
passing. UE4 uses the
GENERATED_IINTERFACE_BODY() macro
which you call in the IFooInterface
definition to add the Execute_*
methods as static methods of your
class. What Execute_DoSomething does
is basically send a message to a
UObject and says “Please see if you
claim to implement the methods in
IFooInterface and if so call the
DoSomething method.” This is kind of
like how classes work in languages
like python or Objective C or
Smalltalk but not how classes
generally work in C++ (which is why I
found everything so confusing).
@anonymous_user_f5a50610,
I do not quite understand what does it mean to work in C++ but not in BP. Do you mean the C++ version of the function will be always running instead of the BP override if you don’t use Execute_ ?
Otherwise can you please give an example where will it not work correctly?
Yes, that because this is cold blood C++ compiled to machine code, if you call function in C++, CPU unconditionally jump to that function code, UE4 don’t have any chance to detect that and intercept that to execute blueprint implementation of interface, or else engine will generate function definition with code calling blueprint function on compile time, which it does not due as it would needlessly complicate use of interface in C++ (but it does that with BlueprintNativeEvent), insted you call generated Execute_ function which checks if there blueprint implementation and executes it if it is or goes call C++ function if it’s not.