Certainly! Another approach to implementing a custom latent blueprint node in Unreal Engine C++ is by using a Blueprint Function Library. Here's an example of how to create a custom latent blueprint node that performs a simple HTTP GET request using a Blueprint Function Library:
- Create a new Blueprint Function Library class file and name it, for example,
MyLatentHttpLibrary.
- In
MyLatentHttpLibrary.h, include the necessary headers:
#include "Kismet/BlueprintFunctionLibrary.h"
#include "MyLatentHttpLibrary.generated.h"
- Declare the custom blueprint function library class:
UCLASS()
class YOURPROJECT_API UMyLatentHttpLibrary : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, Category = "Network")
static void LatentHttpGet(UObject* WorldContextObject, const FString& URL, bool& bSuccess);
};
- Implement the
LatentHttpGet function which will be accessible in Blueprints:
void UMyLatentHttpLibrary::LatentHttpGet(UObject* WorldContextObject, const FString& URL, bool& bSuccess)
{
FLatentActionInfo LatentInfo;
// Register our custom latent action with the game instance
if (UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject))
{
ULatentActionManager* LatentActionManager = World->GetLatentActionManager();
if (LatentActionManager != nullptr)
{
FLatentActionManager& CurrentLatentActionManager = *LatentActionManager;
FName ExecutionFunction = GET_FUNCTION_NAME_CHECKED(UMyLatentHttpLibrary, LatentHttpGet);
LatentInfo = FLatentActionInfo(WorldContextObject, ExecutionFunction, LatentActionManager->FindExistingAction(ExecutionFunction, WorldContextObject));
// Create your own custom latent action class inheriting from FPendingLatentAction and set the desired properties.
FMyHttpGetLatentAction* MyHttpGetAction = new FMyHttpGetLatentAction(URL, bSuccess);
CurrentLatentActionManager.AddNewAction(LatentInfo.CallbackTarget, LatentInfo.UUID, MyHttpGetAction);
}
}
}
- Create your own custom class
FMyHttpGetLatentAction inheriting from FPendingLatentAction to handle the HTTP GET request:
class FMyHttpGetLatentAction : public FPendingLatentAction
{
public:
FString URL;
bool& bSuccess;
FMyHttpGetLatentAction(const FString& InURL, bool& OutSuccess)
: URL(InURL)
, bSuccess(OutSuccess)
{}
void UpdateOperation(FLatentResponse& Response) override
{
// Implement your own HTTP GET request logic here
// Set bSuccess based on the result of the request
bSuccess = MakeHttpGETRequest(URL);
// Complete the latent action
Response.FinishAndTriggerIf(bSuccess, ExecutionFunction, LatentInfo.CallbackTarget);
Response.TriggerLink(ExecutionFunction, LatentInfo.CallbackTarget);
}
};
- Build the project in Visual Studio.
- Open the Blueprint Editor in Unreal Engine.
- Create a new Blueprint or open an existing one.
- In the Blueprint's graph, right-click and search for your custom blueprint function library (
MyLatentHttpLibrary).
- Drag and drop the custom function onto the graph.
- Connect the appropriate execution pins to the inputs and outputs of the custom function.
- Input the desired URL and connect the output execution pins to the desired nodes in the graph.
With the above steps, you have now implemented another approach to create a custom latent blueprint node in Unreal Engine C++ for a simple HTTP GET request using a Blueprint Function Library.