Skip to Content

Simple Navigation Example

This example moves a character toward a world location over multiple frames. The handler returns an FFlayerPromise that completes when the character reaches the target or the request times out.

The example applies direct movement input. Use your game’s navigation system when the character needs pathfinding around obstacles.

#include "GameFramework/Character.h" #include "Flayer/FlayerFunctionLibrary.h" #include "Utils/FlayerFunctionUtils.h" #include "Utils/FlayerPromiseUtils.h" #include "MyNavigationFunctions.generated.h" UCLASS() class UMyNavigationFunctions : public UFlayerFunctionLibrary { GENERATED_BODY() public: UMyNavigationFunctions() { FFlayerFunctionMeta Meta; Meta.PacketName = TEXT("move_to_location"); Meta.Description = TEXT("Moves the player to a world location"); Meta.HintKey = TEXT(".*"); Meta.PromiseReturnType = EParamType::Bool; RegisterCodeFunction(TEXT("MoveToLocation"), Meta); } UFUNCTION() FFlayerPromise MoveToLocation(float X, float Y, float Z, int32 TimeoutMs) { ACharacter* Character = Cast<ACharacter>(UFlayerFunctionUtils::GetPlayerPawn(this)); if (!IsValid(Character)) { return UFlayerPromiseUtils::CreateCompleted( this, FDynamicParams{FDynamicParam(false)} ); } const FVector Target(X, Y, Z); const float AcceptanceRadius = 75.0f; const float TimeoutSeconds = FMath::Max( 0.1f, UFlayerFunctionUtils::MillisecondsToSeconds(TimeoutMs) ); const TWeakObjectPtr<ACharacter> WeakCharacter(Character); FStandardPromiseTickAndResultDelegate OnTick = FStandardPromiseTickAndResultDelegate::CreateLambda( [WeakCharacter, Target, AcceptanceRadius]( float DeltaTime, FDynamicParams& Result ) { if (!WeakCharacter.IsValid()) { Result = FDynamicParams{FDynamicParam(false)}; return true; } const FVector Offset = Target - WeakCharacter->GetActorLocation(); if (Offset.SizeSquared() <= FMath::Square(AcceptanceRadius)) { Result = FDynamicParams{FDynamicParam(true)}; return true; } WeakCharacter->AddMovementInput(Offset.GetSafeNormal()); return false; } ); return UFlayerPromiseUtils::TickingPromiseWithResult( this, OnTick, FDynamicParams{FDynamicParam(false)}, TimeoutSeconds, 0.016f ); } };

The handler declares a Bool promise result and returns one Bool when the character reaches the target. The helper supplies movement input about every 16 milliseconds. If the timeout elapses first, the managed promise reports a timeout error to the caller.

Last updated on