why am I getting this error? LogNetPackageMap: Warning: FNetGUIDCache::SupportsObject: GameplayAbilityPayloadData /Engine/Transient.GameplayAbilityPayloadData_0 NOT Supported. Can you show me c++ code// Copyright 2022 Sabre Dart Studios
The error you are encountering is related to the fact that the GameplayAbilityPayloadData UObject is not supported for replication. UObjects by default cannot be replicated and you will need to use an Actor or ActorComponent instead.
In your case, consider creating a new ActorComponent, e.g., UGameplayAbilityPayloadDataComponent, and then move the replication logic from your UObject to this ActorComponent. The code would look something like this:
Make sure to replace the UObject code with this new ActorComponent. Attach the component to any Actor that needs to store and replicate the GameplayAbilityPayloadDataComponent data.
User
This is just a data class to pass over the network. Is there an easier way to replicate a data class or is this the best way?
Assistant
If you want to pass this data class over the network while avoiding the use of UObject or ActorComponent, you can change the UGameplayAbilityPayloadData class to a simple UStruct, and manage the replication using your own game logic.
Here's a modified version of your class, but as a UStruct:
In this approach, you'll need to manage the replication of the struct data yourself, which could either be done within another Actor or ActorComponent that will handle the struct data replication or by passing the struct as a parameter in a replicated function.
For example, in the Actor/ActorComponent that will replicate the struct data:
Declare a replicated UPROPERTY with your struct.
Override the GetLifetimeReplicatedProps function and add DOREPLIFETIME for your struct.
(Optional) If you need the struct to change during gameplay, create a replicated function to update and replicate the struct value.
User
So the full picture is that I am sending data to my GAS ability through UAbilitySystemBlueprintLibrary::SendGameplayEventToActor(this, AbilityHandleToUse.AbilityTag, GameplayEventDataPayload);
I am setting the OptionalObject as my payload data.
The problem is that my GameplayEventDataPayload is not replicated. I don't want to create an actorcomponent or an actor just to send this simple data class over to my GAS ability. How can I do this in a simple way?
Assistant
Given your use case, you can use an FGameplayEventData struct to send data to the GAS ability. I understand that TWeakObjectPtr properties are not supported in FGameplayEventData, so you can store a reference to the AActor itself, and use casting to access the specific ARagnaAbilTargetActor_GroundTrace actor in the ability.
First, modify your FGameplayAbilityPayloadData struct to store an AActor pointer instead of the ARagnaAbilTargetActor_GroundTrace TWeakObjectPtr:
Next, modify your method for sending the GameplayEvent with the custom payload data:
cpp
User
I am getting this error with this approach LogNetPackageMap: Warning: FNetGUIDCache::SupportsObject: GameplayAbilityPayloadData /Engine/Transient.GameplayAbilityPayloadData_0 NOT Supported.
Assistant
I apologize for the confusion. The usage of UClass pointers can still cause issues when passing them in FGameplayEventData structs. Let's simplify the struct even more by using only basic UPROPERTY types that are supported by FGameplayEventData.
Modify the FGameplayAbilityPayloadData struct to contain an FName variable to store the actor's name instead of storing the actor itself:
Now, update your method for sending the GameplayEvent with the custom payload data:
cpp
FGameplayEventData GameplayEventDataPayload;
GameplayEventDataPayload.EventTag = AbilityHandleToUse.AbilityTag;
FGameplayAbilityPayloadData PayloadData;
PayloadData.SetCurrentAbilityLevel(AbilityHandleToUse.AbilityCurrentLevel);
PayloadData.SetTargetingActorName((Cast<APlayerCharacterGASController>(Controller)->CreateTargetingWidget())->GetFName());
// Store the custom payload data into the InstigatorController
GameplayEventDataPayload.InstigatorController = GetController();
GameplayEventDataPayload.Instigator = GetController()->GetPawn();
// Convert UGameplayAbilityPayloadData to a string and store it as CustomContext
FString PayloadDataString;
PayloadData.ToString(PayloadDataString);
GameplayEventDataPayload.CustomContext = PayloadDataString;
User
In the UGameplayAbilityPayloadData class to be replicated
Assistant
I apologize for the confusion earlier. If you need to replicate the custom data through GAS as an object, consider using an FGameplayTagContainer to store the custom data and pass it to the GAS ability using the FGameplayEventData struct. You can append custom tags to the FGameplayEventData's Tags property, which is replicated by default.
To use this method, do the following:
Modify your UGameplayAbilityPayloadData class to use functions that will generate FGameplayTags based on the custom data:
GameplayAbilityPayloadData.h
cpp
// Copyright 2022 Sabre Dart Studios
#pragma once
#include "CoreMinimal.h"
#include "GameplayTagContainer.h"
#include "RagnaAbilTargetActor_GroundTrace.h"
#include "GameplayAbilityPayloadData.generated.h"
UCLASS(BlueprintType)
class OPENWORLDSTARTER_API UGameplayAbilityPayloadData : public UObject
{
GENERATED_BODY()
public:
UGameplayAbilityPayloadData();
int GetCurrentAbilityLevel() const { return CurrentAbilityLevel; }
void SetCurrentAbilityLevel(int val) { CurrentAbilityLevel = val; }
TWeakObjectPtr<ARagnaAbilTargetActor_GroundTrace> GetTargetingActor() const { return TargetingActor; }
void SetTargetingActor(TWeakObjectPtr<ARagnaAbilTargetActor_GroundTrace> val) { TargetingActor = val; }
// Function to append data to a GameplayTagContainer
UFUNCTION(BlueprintCallable, Category = "Ability Payload Data")
void AppendDataToTagContainer(FGameplayTagContainer& TagContainer) const;
private:
int CurrentAbilityLevel = 0;
TWeakObjectPtr<ARagnaAbilTargetActor_GroundTrace> TargetingActor = nullptr;
};
GameplayAbilityPayloadData.cpp
cpp
User
What's the easiest way to replicate a UObject?
Assistant
Unfortunately, UObject does not directly support replication because it is not derived from an Actor or ActorComponent. However, you can replicate UObject data using a simple workaround by creating a replicated ActorComponent that stores and replicates the UObject's data. The ActorComponent can serve as an intermediary between UObject and other actors in your game that need the replicated data.
Here's an example:
ReplicatedUObjectDataComponent.h
cpp
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "GameplayAbilityPayloadData.h"
#include "ReplicatedUObjectDataComponent.generated.h"
UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class OPENWORLDSTARTER_API UReplicatedUObjectDataComponent : public UActorComponent
{
GENERATED_BODY()
public:
UReplicatedUObjectDataComponent();
// Function to set UObject data
UFUNCTION(BlueprintCallable, Category = "UObject Replication")
void SetUObjectData(UGameplayAbilityPayloadData* NewData);
// Function to get UObject data
UFUNCTION(BlueprintCallable, Category = "UObject Replication")
UGameplayAbilityPayloadData* GetUObjectData() const;
protected:
virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;
private:
UPROPERTY(ReplicatedUsing = OnRep_UObjectData)
UGameplayAbilityPayloadData* UObjectData;
UFUNCTION()
void OnRep_UObjectData();
};
ReplicatedUObjectDataComponent.cpp
cpp
User
LogNetPackageMap: Warning: FNetGUIDCache::SupportsObject: GameplayAbilityPayloadComponent /Engine/Transient.GameplayAbilityPayloadComponent_25 NOT Supported. I am getting this error
Assistant
I apologize for the confusion earlier. Unfortunately, UObject and its derived classes still have issues with replication in Unreal even when using an ActorComponent as a workaround. The best approach is to refactor your UObject into a UStruct, and handle replication manually.
By refactoring your UObject to a UStruct (FGameplayAbilityPayloadData) and storing the data inside the FGameplayEventData, you can efficiently send the data to your GAS ability without running into replication issues related to UObject lifecycles.
With this setup, you can attach the UGameplayAbilityPayloadDataHandlerComponent to any Actor that needs to store and replicate FGameplayAbilityPayloadData, and you can use the UpdateAndReplicatePayloadData function to update and send the data to all clients.
FGameplayEventData GameplayEventDataPayload;
GameplayEventDataPayload.EventTag = AbilityHandleToUse.AbilityTag;
FGameplayAbilityPayloadData PayloadData;
PayloadData.SetCurrentAbilityLevel(AbilityHandleToUse.AbilityCurrentLevel);
PayloadData.SetTargetingActor((Cast<APlayerCharacterGASController>(Controller)->CreateTargetingWidget()));
// Store the custom payload data into the InstigatorController
GameplayEventDataPayload.InstigatorController = GetController();
GameplayEventDataPayload.Instigator = GetController()->GetPawn();
// Convert UGameplayAbilityPayloadData to a string and store it as CustomContext
FString PayloadDataString;
PayloadData.ToString(PayloadDataString);
GameplayEventDataPayload.CustomContext = PayloadDataString;
In your GAS ability, you can now read the GameplayEvent's InstigatorController and CustomContext to access the custom payload data. When the GameplayEvent is triggered, you can extract the FGameplayAbilityPayloadData from the CustomContext string:
cpp
void UYourGameplayAbility::OnGameplayEvent_Implementation(FGameplayEventData Payload)
{
// Extract the FGameplayAbilityPayloadData from CustomContext
FGameplayAbilityPayloadData CustomPayloadData;
if (Payload.CustomContext.IsEmpty())
{
UE_LOG(LogTemp, Warning, TEXT("CustomContext is empty. Could not extract payload data."));
return;
}
if (!CustomPayloadData.InitFromString(Payload.CustomContext))
{
UE_LOG(LogTemp, Warning, TEXT("Could not init payload data from string."));
return;
}
// Use the custom payload data in your ability
int CurrentAbilityLevel = CustomPayloadData.GetCurrentAbilityLevel();
// Cast the AActor to your specific actor class (e.g., ARagnaAbilTargetActor_GroundTrace)
ARagnaAbilTargetActor_GroundTrace* TargetingActor = Cast<ARagnaAbilTargetActor_GroundTrace>(CustomPayloadData.GetTargetingActor());
// Continue using TargetingActor and CurrentAbilityLevel in your ability as needed
}
With this approach, you don't need to create ActorComponent, while still being able to send the custom payload data in a GAS ability without UObject.
Keep in mind that the Payload.MakeArray() function returns a TArray which can lose type information. If you need detailed information, it's best to look into the InstigatorController and CustomContext of the FGameplayEventData.
In your GAS ability, you can now read the GameplayEvent's InstigatorController and CustomContext to access the custom payload data. When the GameplayEvent is triggered, you can extract the FGameplayAbilityPayloadData from the CustomContext string and use the FName to find the actor:
cpp
void UYourGameplayAbility::OnGameplayEvent_Implementation(FGameplayEventData Payload)
{
// Extract the FGameplayAbilityPayloadData from CustomContext
FGameplayAbilityPayloadData CustomPayloadData;
if (Payload.CustomContext.IsEmpty())
{
UE_LOG(LogTemp, Warning, TEXT("CustomContext is empty. Could not extract payload data."));
return;
}
if (!CustomPayloadData.InitFromString(Payload.CustomContext))
{
UE_LOG(LogTemp, Warning, TEXT("Could not init payload data from string."));
return;
}
// Use the custom payload data in your ability
int CurrentAbilityLevel = CustomPayloadData.GetCurrentAbilityLevel();
// Find the actor by name and cast it to your specific actor class (e.g., ARagnaAbilTargetActor_GroundTrace)
FName TargetingActorName = CustomPayloadData.GetTargetingActorName();
for (TActorIterator<ARagnaAbilTargetActor_GroundTrace> ActorItr(GetWorld()); ActorItr; ++ActorItr)
{
if (ActorItr->GetFName() == TargetingActorName)
{
ARagnaAbilTargetActor_GroundTrace* TargetingActor = *ActorItr;
// Use the TargetingActor in your ability as needed
break;
}
}
}
This approach will ensure that only supported basic data types are used in the struct and sent through the GameplayEvent, so you don't run into replication issues.
// Copyright 2022 Sabre Dart Studios
#include "GameplayAbilityPayloadData.h"
UGameplayAbilityPayloadData::UGameplayAbilityPayloadData()
{
}
void UGameplayAbilityPayloadData::AppendDataToTagContainer(FGameplayTagContainer& TagContainer) const
{
// Example: convert the CurrentAbilityLevel into FString and store it as a tag
FString TagString = FString::Printf(TEXT("CustomPayload.CurrentAbilityLevel_%d"), CurrentAbilityLevel);
FGameplayTag NewTag = FGameplayTag::RequestGameplayTag(FName(*TagString));
TagContainer.AddTag(NewTag);
// Example: convert the TargetingActor's unique ID into FString and store it as a tag
if (TargetingActor.IsValid())
{
TagString = FString::Printf(TEXT("CustomPayload.TargetingActor_%d"), TargetingActor->GetUniqueID());
NewTag = FGameplayTag::RequestGameplayTag(FName(*TagString));
TagContainer.AddTag(NewTag);
}
}
Send the custom data in a GAS ability by appending it to the FGameplayEventData's Tags property:
In your GAS ability, access the custom Tags from the FGameplayEventData struct when the GameplayEvent is triggered:
cpp
void UYourGameplayAbility::OnGameplayEvent_Implementation(FGameplayEventData Payload)
{
FGameplayTagContainer CustomTags = Payload.Tags;
// Example: extract the CurrentAbilityLevel from the custom tags
int CurrentAbilityLevel = -1;
for (const FGameplayTag& Tag : CustomTags)
{
FString TagString = Tag.GetTagName().ToString();
if (TagString.StartsWith("CustomPayload.CurrentAbilityLevel_"))
{
FString LevelString = TagString.RightChop(32); // Remove the prefix
CurrentAbilityLevel = FCString::Atoi(*LevelString); // Convert string to int
break;
}
}
// Example: extract the TargetingActor's ID and find the actor in the world
int32 TargetingActorID = -1;
for (const FGameplayTag& Tag : CustomTags)
{
FString TagString = Tag.GetTagName().ToString();
if (TagString.StartsWith("CustomPayload.TargetingActor_"))
{
FString IDString = TagString.RightChop(27); // Remove the prefix
TargetingActorID = FCString::Atoi(*IDString); // Convert string to int
break;
}
}
if (TargetingActorID != -1)
{
// Find the actor by ID and cast it to your specific actor class (e.g., ARagnaAbilTargetActor_GroundTrace)
for (TActorIterator<ARagnaAbilTargetActor_GroundTrace> ActorItr(GetWorld()); ActorItr; ++ActorItr)
{
if (ActorItr->GetUniqueID() == TargetingActorID)
{
ARagnaAbilTargetActor_GroundTrace* TargetingActor = *ActorItr;
// Use the TargetingActor and CurrentAbilityLevel in your ability as needed
break;
}
}
}
}
By using the FGameplayTagContainer, you can replicate the custom payload data without needing UObject or USTRUCT. Just make sure to parse the tags correctly based on the custom payload data you stored in them.
#include "ReplicatedUObjectDataComponent.h"
#include <Net/UnrealNetwork.h>
UReplicatedUObjectDataComponent::UReplicatedUObjectDataComponent()
{
SetIsReplicated(true);
}
void UReplicatedUObjectDataComponent::SetUObjectData(UGameplayAbilityPayloadData* NewData)
{
UObjectData = NewData;
OnRep_UObjectData();
}
UGameplayAbilityPayloadData* UReplicatedUObjectDataComponent::GetUObjectData() const
{
return UObjectData;
}
void UReplicatedUObjectDataComponent::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(UReplicatedUObjectDataComponent, UObjectData);
}
void UReplicatedUObjectDataComponent::OnRep_UObjectData()
{
// Insert any code that should be executed when the UObject data is replicated (e.g., updating UI, triggering events, etc.)
}
With this setup, you can attach the UReplicatedUObjectDataComponent to any Actor that needs to store and replicate a UObject. The ActorComponent will replicate the UObject data and allow you to synchronize the information across clients. When you need to access or modify the UObject data, use the SetUObjectData and GetUObjectData functions provided.
However, the ideal approach would still be to refactor your UObject into an Actor, ActorComponent, or UStruct where replication is more straightforward and better supported. Using the workaround will come with certain limitations (e.g., UObject lifecycle management, increased complexity of the code).