Skip to content

Instantly share code, notes, and snippets.

@redxdev
Created April 27, 2021 22:21
Show Gist options
  • Save redxdev/ddc26a42f1827acd7b5bb11592be996e to your computer and use it in GitHub Desktop.
Save redxdev/ddc26a42f1827acd7b5bb11592be996e to your computer and use it in GitHub Desktop.
Blueprint spawnable smart nav link component
#include "FlameSmartNavLinkComponent.h"
#include "Navigation/PathFollowingComponent.h"
UFlameSmartNavLinkComponent::UFlameSmartNavLinkComponent()
{
SetMoveReachedLink(this, &ThisClass::NotifySmartLinkReached);
}
void UFlameSmartNavLinkComponent::ResumePathFollowing(AActor* Agent)
{
if (Agent)
{
UPathFollowingComponent* PathComp = Agent->FindComponentByClass<UPathFollowingComponent>();
if (!PathComp)
{
APawn* PawnOwner = Cast<APawn>(Agent);
if (PawnOwner && PawnOwner->GetController())
{
PathComp = PawnOwner->GetController()->FindComponentByClass<UPathFollowingComponent>();
}
}
if (PathComp)
{
PathComp->FinishUsingCustomLink(this);
}
}
}
void UFlameSmartNavLinkComponent::K2_SetEnabled(bool bNewEnabled)
{
SetEnabled(bNewEnabled);
}
void UFlameSmartNavLinkComponent::NotifySmartLinkReached(UNavLinkCustomComponent* LinkComp, UObject* PathingAgent, const FVector& DestPoint)
{
UPathFollowingComponent* PathComp = Cast<UPathFollowingComponent>(PathingAgent);
if (PathComp)
{
AActor* PathOwner = PathComp->GetOwner();
AController* ControllerOwner = Cast<AController>(PathOwner);
if (ControllerOwner)
{
PathOwner = ControllerOwner->GetPawn();
}
OnSmartLinkReached.Broadcast(PathOwner, DestPoint);
}
}
#include "CoreMinimal.h"
#include "NavLinkCustomComponent.h"
#include "FlameSmartNavLinkComponent.generated.h"
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FSmartNavLinkReachedDelegate, AActor*, Agent, const FVector&, Destination);
/**
* Equivalent to the "smart link" functionality of ANavLinkProxy, but with support for adding it to custom blueprints.
*/
UCLASS(ClassGroup = "Navigation", meta = (BlueprintSpawnableComponent))
class FLAME_API UFlameSmartNavLinkComponent : public UNavLinkCustomComponent
{
GENERATED_BODY()
public:
UFlameSmartNavLinkComponent();
UPROPERTY(BlueprintAssignable)
FSmartNavLinkReachedDelegate OnSmartLinkReached;
UFUNCTION(BlueprintCallable, Category = "AI|Navigation")
void ResumePathFollowing(AActor* Agent);
UFUNCTION(BlueprintCallable, Category = "AI|Navigation", meta = (DisplayName = "Set Enabled"))
void K2_SetEnabled(bool bNewEnabled);
UFUNCTION(BlueprintCallable, Category = "AI|Navigation", meta = (DisplayName = "Is Enabled"))
FORCEINLINE bool K2_IsEnabled() const { return IsEnabled(); }
UFUNCTION(BlueprintCallable, Category = "AI|Navigation", meta = (DisplayName = "Has Moving Agents"))
FORCEINLINE bool K2_HasMovingAgents() const { return HasMovingAgents(); }
private:
void NotifySmartLinkReached(UNavLinkCustomComponent* LinkComp, UObject* PathingAgent, const FVector& DestPoint);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment