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.
The solution is basically message
passing. UE4 uses the
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).
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.