Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Use of Execute_Prefix for Interfaces

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:

 IIdentityActorComponentDataInterface* ActorComponentDataInterface = Cast<IIdentityActorComponentDataInterface>(Character);

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.

Maybe someone could clarify it.

Product Version: UE 4.18
more ▼

asked Nov 12 '17 at 07:29 PM in C++ Programming

avatar image

Martin Egger
388 17 10 29

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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.

more ▼

answered Nov 12 '17 at 08:05 PM

avatar image

37.3k 936 172 1116

avatar image Martin Egger Nov 13 '17 at 09:22 PM

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:

 class UIdentityActorComponentDataInterface : public UInterface
 class IIdentityActorComponentDataInterface
 /* Defaults                                                             */
     * Initializes and loads all data from the Component
     virtual void InitializeActorComponent(TSubclassOf<UIdentityParadigmComponent> ParadigmComponent);
     virtual void InitializeActorComponent(TSubclassOf<UIdentityWeaponComponent> WeaponComponent);
     virtual void InitializeActorComponent();

Also I will not use it in Blueprint at all.

avatar image Martin Egger Nov 13 '17 at 11:07 PM

Just as a thought.

Is Execute_Prefix only if I use actually a BlueprintNativeImplementation as it will call than Blueprint but pure C++ calls it as I already done it?

Looking at https://answers.unrealengine.com/questions/250263/calling-interface-functions-in-c.html

avatar image Martin Egger Nov 14 '17 at 09:32 PM

Found my answer in https://answers.unrealengine.com/questions/214147/grand-unified-cblueprint-cast-interface-explanatio.html

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).

avatar image Arty-McLabin Mar 25 '18 at 07:57 PM

@Shadowriver, 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?

avatar image Shadowriver Nov 22 '18 at 01:00 AM

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.

avatar image Arty-McLabin Nov 22 '18 at 11:15 AM

makes perfect sense, thanks for the clarification mate :]

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question