Hey, I noticed a lot of people have this problem.
Let’s brake it down to a simple digestible bits:
- How to call c++ code (function) in blueprint:
I assume you can learn c++ class structure on your own.
Function in *.h file can be prefixed with UFUNCTION() macro with certain parameters in prentices. It looks like this:
UCLASS(Blueprintable)
class MODULE_API UMyObject : public UObject
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, Category = "my functions")
void MyCppFunctionWithExecutionFlow();
UFUNCTION(BlueprintPure, Category = "my functions")
void MyCppFunctionWithoutExecutionFlow();
};
now it blueprint our object UMyObject instance will have both MyCppFunctionWithExecutionFlow and MyCppFunctionWithoutExecutionFlow.
2 where to put the code. I recommend to create plugin as BlueprintFunctionLibrary and store all such code there
Plugin is very easy to create from UEeditor->edit-> plugins ->Add. you’ll get something like this
UCLASS()
class UBFLTestBPLibrary : public UBlueprintFunctionLibrary
{
GENERATED_UCLASS_BODY()
UFUNCTION(BlueprintCallable, meta = (DisplayName = "Execute Sample function", Keywords = "BFLTest sample test testing"), Category = "BFLTestTesting")
static float BFLTestSampleFunction(float Param);
};
noticed that in *.h file return value of functions prefixed with keyword “static” this mean that in order to call this function you do not need instance of object it is implemented in.
you can create any function this way as long as these functions does not need to store data between calls (this is very simplistic).
3 Modules. Functionality in UE divided into modules and in order to asscess it you need to include module you need in *.Build.cs with located in plugin source folder. Module name can be found in ue documentation.
public class BFLTest : ModuleRules
{
public BFLTest(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs;
PublicIncludePaths.AddRange(
new string[] {
// ... add public include paths required here ...
}
);
PrivateIncludePaths.AddRange(
new string[] {
// ... add other private include paths required here ...
}
);
PublicDependencyModuleNames.AddRange(
new string[]
{
"Core",
// ... add other public dependencies that you statically link with here ...
}
);
PrivateDependencyModuleNames.AddRange(
new string[]
{
"CoreUObject",
"Engine",
"Slate",
"SlateCore",
// ... add private dependencies that you statically link with here ...
}
);
DynamicallyLoadedModuleNames.AddRange(
new string[]
{
// ... add any modules that your module loads dynamically here ...
}
);
}
}
after adding module and relevant includes better to clean derivative data and regenerate vs/xcode project and recompile it.
Of course it is much more to this and it might not work from first attempt but this is better than nothing.