Skip to content

Instantly share code, notes, and snippets.

@CoffeeVampir3
Created October 16, 2023 05:40
Show Gist options
  • Save CoffeeVampir3/8d669c45fb8a692e9c203b6aa143951c to your computer and use it in GitHub Desktop.
Save CoffeeVampir3/8d669c45fb8a692e9c203b6aa143951c to your computer and use it in GitHub Desktop.
Wait Target Data Under Cursor C++ UE 5.3
#include "AbilitySystem/AbilityTasks/TargetDataUnderMouse.h"
#include "AbilitySystemComponent.h"
UTargetDataUnderMouse* UTargetDataUnderMouse::CreateTargetDataUnderMouse(UGameplayAbility* OwningAbility)
{
UTargetDataUnderMouse* ConstructedObj = NewAbilityTask<UTargetDataUnderMouse>(OwningAbility);
return ConstructedObj;
}
void UTargetDataUnderMouse::Activate()
{
const bool bIsLocallyControlled = Ability->GetCurrentActorInfo()->IsLocallyControlled();
if(bIsLocallyControlled)
{
SendMouseCursorData();
} else
{
const auto SpecHandle = GetAbilitySpecHandle();
const auto ActivationKey = GetActivationPredictionKey();
//Bind to the target data delegate.
AbilitySystemComponent.Get()->AbilityTargetDataSetDelegate(SpecHandle, ActivationKey)
.AddLambda([this, SpecHandle, ActivationKey](const FGameplayAbilityTargetDataHandle& DataHandle, FGameplayTag ActivationTag)
{
AbilitySystemComponent->ConsumeClientReplicatedTargetData(SpecHandle, ActivationKey);
if(ShouldBroadcastAbilityTaskDelegates())
{
ValidMouseTargetData.Broadcast(DataHandle);
}
});
//In case our data arrived before the delegate was bound, we can ensure we retrieve the data here.
const bool bDelegateWasCalled = AbilitySystemComponent.Get()->
CallReplicatedTargetDataDelegatesIfSet(SpecHandle, ActivationKey);
if (!bDelegateWasCalled)
{
SetWaitingOnRemotePlayerData();
}
}
}
void UTargetDataUnderMouse::SendMouseCursorData() const
{
//Creates a prediction window for this function, which fills in the scoped prediction key.
FScopedPredictionWindow ScopedPrediction(AbilitySystemComponent.Get());
const auto PlayerController = Ability->GetCurrentActorInfo()->PlayerController.Get();
if(!PlayerController)
return;
FHitResult CursorHit;
PlayerController->GetHitResultUnderCursor(ECC_Visibility, false, CursorHit);
FGameplayAbilityTargetDataHandle DataHandle;
const auto TargetData = new FGameplayAbilityTargetData_SingleTargetHit();
TargetData->HitResult = CursorHit;
DataHandle.Add(TargetData);
AbilitySystemComponent->ServerSetReplicatedTargetData(
GetAbilitySpecHandle(),
GetActivationPredictionKey(),
DataHandle,
FGameplayTag(),
AbilitySystemComponent->ScopedPredictionKey);
if(!ShouldBroadcastAbilityTaskDelegates()) return;
ValidMouseTargetData.Broadcast(DataHandle);
}
#pragma once
#include "CoreMinimal.h"
#include "Abilities/Tasks/AbilityTask.h"
#include "TargetDataUnderMouse.generated.h"
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FMouseTargetDataSignature, const FGameplayAbilityTargetDataHandle&, TargetDataHandle);
/**
*
*/
UCLASS()
class AURAPROJECT_API UTargetDataUnderMouse : public UAbilityTask
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, Category="Ability|Tasks|TargetData",
meta=(DisplayName = "TargetDataUnderMouse", HidePin="OwningAbility", DefaultToSelf="OwningAbility",
BlueprintInternalUseOnly="true"))
static UTargetDataUnderMouse* CreateTargetDataUnderMouse(UGameplayAbility* OwningAbility);
UPROPERTY(BlueprintAssignable)
FMouseTargetDataSignature ValidMouseTargetData;
private:
virtual void Activate() override;
void SendMouseCursorData() const;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment