UE4 Blueprints, Creating C++ Functions as new Blueprint Nodes

https://wiki.unrealengine.com/Blueprints,_Creating_C%2B%2B_Functions_as_new_Blueprint_Nodes

Blueprints, Creating C++ Functions as new Blueprint Nodes


Overview

Original Author: Rama (talk)
Dear Community,
This is an explanation of the syntax for a C++ function that you want to also run from blueprints!

BlueprintCallable

A BlueprintCallable function will have execution pins so that you can chain your C++ functions together in UE4 Blueprints.
/** Activate Fancy New Project-Specific System Created in C++! */
UFUNCTION(BlueprintCallable, Category="SunShine")
void ActivateSunShine();
Sunshine.jpg

BlueprintPure

A great efficiency for blueprint users is to be able to access data from your C++ system without having to run the exec chain through it. If a function does not modify the game state, which requires a certain ordering for the rest of Blueprint logic and game logic to occurr correctly, then you can use Blueprint pure!
A simple test for BlueprintPure is to use it for accessor functions, which are not modifying any variable data at all.
So if you are wondering if you can make a function BlueprintPure, just ask yourself.
"Is this function entirely just an accessor for data, or doing calculations which have no bearing on the rest of the game state?"
/** What is the current Sun Shine Rotation? ! */
UFUNCTION(BlueprintPure, Category="SunShine")
FRotator GetSunShineRotation();
Newbpnodes.jpg

Static

If your functionality is not for a specific actor, or the functionality relates to a specific actor class but does not use member variables, then you can make the function static so that the BP node will show up anywhere in any BP graph:
/** Log sunshine status from any BP in your entire game code! */
UFUNCTION(BlueprintCallable, Category="SunShine")
static void LogSunShineStatus();

Static With World Context

If you want to use a static BP node anywhere in the game code, but your C++ function wants to modify the state of the game world by creating objects or actors, or performing an action on any instanced data within the game world, then you can pass along a world context object which is an invisible input parameter:
/** Make sure to save off the return value as a global variable in one of your BPs or else it will get garbage collected! */
UFUNCTION(BlueprintCallable, Category = "Victory BP Library", meta = (WorldContext = "WorldContextObject"))
static UPrimitiveComponent* CreatePrimitiveComponent(UObject* WorldContextObject, TSubclassOf<UPrimitiveComponent> CompClass, FName Name, FVector Location, FRotator Rotation);
UPrimitiveComponent* UVictoryBPFunctionLibrary::CreatePrimitiveComponent(
        UObject* WorldContextObject, 
 TSubclassOf<UPrimitiveComponent> CompClass, 
 FName Name,
 FVector Location, 
 FRotator Rotation
){
 if(!CompClass) 
 {
  return nullptr;
 }
 //~~~~~~~~~~~~~~~~~
 
 //using a context object to get the world!
        UWorld* const World = GEngine->GetWorldFromContextObject(WorldContextObject);
 if(!World) 
 {
  return nullptr;
 }
 //~~~~~~~~~~~
 
 UPrimitiveComponent* NewComp = NewObject<UPrimitiveComponent>(World, Name);
 if(!NewComp) 
 {
  return nullptr;
 }
 //~~~~~~~~~~~~~
 
 NewComp->SetWorldLocation(Location);
 NewComp->SetWorldRotation(Rotation);
 NewComp->RegisterComponentWithWorld(World);
 
 return NewComp;
}

Creating Interface for BP Users To Access New C++ Systems

This is how you can create the interface in c++ for blueprint users to access your C++ systems!
You can write all the core code in c++, and then give the rest of your team the power to access, modify, and utilize your awesome new C++ system!

댓글

이 블로그의 인기 게시물

About AActor!!! "UObject" has no member "BeginPlay"

UNREAL Android build information

C++ 생성자 위임 (delegating constructor)