The use of UE4 interface than ordinary high-level language, to do a lot more work, because to be compatible with the use of blueprints, there are some pits need to pay attention to the beginning of it.
1. New Interface class
Open the UE4 editor, as usual, create a new C + + class, then choose Object Inheritance, and then take the name, here I use my interface name in the work, as an example, "Itrackteam"
And then modify the inherited class, and you do not read wrong, is to write 2 class, a call Utrackteam, a call Itrackteam, the class name and header file name can be different, my side of the name does not follow the rules of the unreal, it should be named "Uitrackteam" and " Iitrackteam ".
. h file
#pragmaOnce#include"Object.h"#include"itrackteam.generated.h"uinterface (blueprintable)classUtrackteam: Publicuinterface{generated_uinterface_body ()};classitrackteam{generated_iinterface_body () Public: /*get the following object in front of you*/ufunction (blueprintnativeevent, blueprintcallable, Category="Item") Aactor*Getpremember (); /*get position following target point*/ufunction (blueprintnativeevent, blueprintcallable, Category="Item") Fvector Gettaillocation (Int32&PARAM1,BOOLparam2);};
. cpp file
" Programming.h " "ITrackTeam.h"utrackteam::utrackteam (const fobjectinitializer& Objectinitializer): Super (Objectinitializer) {}
The 2.c++ class implements this interface
I have an actor that implements the interface, "Aimplementinterfaceactor", and then references the header file first.
" interface/itrackteam.h "
Then inherit the interface class
class Public Public Itrackteam
Implement the interface, note that after the original method name, add "_implementation"
Virtual Override ; Virtual BOOL override;
. cpp file, you can write a test statement so voluminous that it can be called by the blueprint.
aactor*aimplementinterfaceactor::getpremember_implementation () {Aactor*temp =nullptr; returntemp;} Fvector Aimplementinterfaceactor::gettaillocation_implementation (Int32&PARAM1,BOOLparam2) {Gengine->addonscreendebugmessage (-1,5.0f, fcolor::red, Fstring::fromint (param1)); if(param2) {Gengine->addonscreendebugmessage (-1,5.0f, Fcolor::red,text ("True") ); } param1=7; returnFvector (0.0f,1.0f,2.0f);}
3.c++, send an interface message to a class
First into the interface class, and then call the method, call attention, must use "Execute_" + method name, in parentheses, if it is a method without parameters, directly write the original target class pointer, if there are parameters, such as a int32 and bool, attached to write in the back on it.
aactor* temp = Nullptr;itrackteam* iTemp = cast<itrackteam> (this); if (iTemp) { 5; = Itemp->execute_gettaillocation (Thistrue); Gengine->addonscreendebugmessage (-15.0f, fcolor::red, tmpvector.tostring ()); Gengine->addonscreendebugmessage (-15.0f, fcolor::red, Fstring::fromint ( Refint));}
4. Determine if a particular class implements an interface
To ensure compatibility between C + + and the Blueprint class that implements the interface, use the following code:
Obj->getclass ()->implementsinterface (Uiteminterface::staticclass ());
Other links.
Official Document: Https://docs.unrealengine.com/latest/CHN/Programming/UnrealArchitecture/Reference/Interfaces/index.html
Wiki:https://wiki.unrealengine.com/interfaces_in_c%2b%2b
Question and Answer: https://answers.unrealengine.com/questions/250263/calling-interface-functions-in-c.html
Ue4 C + + interface