Im working on a replicated inventory component with replicated UItems array, where UItems inherit from UDataAsset.
I spawn a pick up actor which holds the UItem and when I pick up multiple items of same kind they start to share the same values such as int32 inventory index and int32 amount.
UItems of same kind have same NetPushID_Internal values and memory address when i checked in VS, I think this is due to the UItems not being instantiated or coded properly by me yet.
How do i replicated my UItems properly?
#include "Items/Item.h"
#include "Net/UnrealNetwork.h"
bool UInventoryItem::IsConsumable() const
{
// if (MaxCount > 0)
// {
return true;
// }
// return false;
}
FString UInventoryItem::GetIdentifierString() const
{
return GetPrimaryAssetId().ToString();
}
FPrimaryAssetId UInventoryItem::GetPrimaryAssetId() const
{
// This is a DataAsset and not a blueprint so we can just use the raw FName
// For blueprints you need to handle stripping the _C suffix
return FPrimaryAssetId(ItemType, GetFName());
}
bool UInventoryItem::ReplicateSubobjects(UActorChannel* Channel, FOutBunch* Bunch, FReplicationFlags* RepFlags)
{
return false;
}
void UInventoryItem::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
UBlueprintGeneratedClass * bpClass = Cast<UBlueprintGeneratedClass>(this->GetClass());
if (bpClass != nullptr)
{
bpClass->GetLifetimeBlueprintReplicationList(OutLifetimeProps);
}
DOREPLIFETIME(UInventoryItem, StackCount);
DOREPLIFETIME(UInventoryItem, InventoryIndex);
}
void UInventoryItem::SetNetAddressable()
{
bNetAddressable = true;
}
// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "EMSActorSaveInterface.h"
#include "Engine/DataAsset.h"
#include "Styling/SlateBrush.h"
#include "MPAssetManager.h"
#include "Item.generated.h"
class UInventoryComponent;
enum class EALSOverlayState : uint8;
class UGameplayAbility;
/** Base class for all items, do not blueprint directly */
UCLASS(Abstract, BlueprintType)
class MULTIPLAYERGAME_API UInventoryItem : public UPrimaryDataAsset, public IEMSActorSaveInterface
{
GENERATED_BODY()
public:
/** Type of this item, set in native parent class */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Item)
FPrimaryAssetType ItemType;
/** User-visible short name */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Item)
FText ItemName;
/** User-visible long description */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Item)
FText ItemDescription;
/** Icon to display */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Item)
FSlateBrush ItemIcon;
/** Price in game */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Item)
int32 Price=1;
/** Current number of instances that's in the inventory, <= 0 means infinite */
UPROPERTY(EditAnywhere, Replicated, BlueprintReadWrite)
int32 StackCount = 0;
/** Maximum number of instances that can be in inventory at once, <= 0 means infinite */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Max)
int32 MaxCount = 0;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Replicated, Category = InventoryIndex)
int32 InventoryIndex = -1;
/** Returns if the item is consumable (MaxCount <= 0)*/
UFUNCTION(BlueprintCallable, BlueprintPure, Category = Max)
bool IsConsumable() const;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
EALSOverlayState ALSOverlayState;
/** Maximum level this item can be, <= 0 means infinite */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Max)
int32 MaxLevel=1;
/** Ability to grant if this item is slotted */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Abilities)
TSubclassOf<UGameplayAbility> GrantedAbility;
/** Ability level this item grants. <= 0 means the character level */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Abilities)
int32 AbilityLevel=1;
/** Returns the logical name, equivalent to the primary asset id */
UFUNCTION(BlueprintCallable, Category = Item)
FString GetIdentifierString() const;
/** Overridden to use saved type */
virtual FPrimaryAssetId GetPrimaryAssetId() const override;
UPROPERTY(BlueprintReadOnly, Category = "Inventory")
TObjectPtr<UInventoryComponent> InventoryComponent;
//Replication
virtual bool IsSupportedForNetworking() const override
{
return true;
}
virtual bool ReplicateSubobjects(class UActorChannel* Channel, class FOutBunch* Bunch, FReplicationFlags* RepFlags);
virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;
void SetNetAddressable();
uint32 bNetAddressable : 1;
};