Basically the title. I have an InventoryComponent
, which holds a pointer to a custom UInventory
class. This inventory class contains a TArray which should be replicated. However, the pointer to the UInventory
is always NULL. No idea why.
Code snippets:
UInventory:
UCLASS()
class UInventory : public UObject
{
GENERATED_BODY()
private:
UPROPERTY(ReplicatedUsing=OnRep_InventoryArray)
TArray<FInventoryEntry> mInventoryArray;
UPROPERTY()
TMap<FName, int32> mLookupCache;
UFUNCTION()
void RebuildCache();
UFUNCTION()
void OnRep_InventoryArray();
public:
UInventory();
TTuple<EChangeGroupStatus, TArray<EChangeStatus>> ModifyGroupOfEntries(const TArray<FInventoryEntry>& inventoryChanges);
TTuple<EChangeGroupStatus, TArray<EChangeStatus>> ModifyGroupOfEntries(const TMap<FName, int32>& inventoryChanges);
void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;
virtual bool IsSupportedForNetworking() const override { return true; }
UFUNCTION(Category = "Networked Inventory")
EAddStatus AddNewEntry(const FInventoryEntry& entry);
UFUNCTION(Category = "Networked Inventory")
EChangeStatus ModifyEntry(const FInventoryEntry& entryChange);
UFUNCTION(Category = "Networked Inventory")
TArray<ERemovalStatus> RemoveGroupOfItems(const TArray<FName>& itemsToRemove);
UFUNCTION(Category = "Networked Inventory")
ERemovalStatus RemoveItem(const FName itemCode);
UFUNCTION(Category = "Networked Inventory")
int32 GetQuantityFor(const FName itemCode) const;
UFUNCTION(Category = "Networked Inventory")
bool Contains(const FName itemCode) const;
UFUNCTION(BlueprintCallable, Category = "Networked Inventory")
int32 Num() const;
UFUNCTION(BlueprintCallable, Category = "Networked Inventory")
FString ToString() const;
};
UInventoryComponent:
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class NETWORKEDINVENTORY_API UInventoryComponent : public UActorComponent, public IInventoryInterface
{
GENERATED_BODY()
private:
UPROPERTY(Replicated, meta = (AllowPrivateAccess = true))
UInventory* mInventory;
public:
UInventoryComponent();
void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;
virtual bool IsSupportedForNetworking() const override { return true; }
virtual bool ReplicateSubobjects(UActorChannel* Channel, FOutBunch* Bunch, FReplicationFlags* RepFlags) override;
//virtual void OnSubobjectCreatedFromReplication(UObject* NewSubobject) override;
UFUNCTION(Category = "Networked Inventory")
virtual void ModifyInventory(const TArray<FInventoryEntry>& inventoryChanges) override;
UFUNCTION(Category = "Networked Inventory")
virtual void AddItemsToInventory(const TArray<FInventoryEntry>& inventoryChanges) override;
UFUNCTION(Category = "Networked Inventory")
virtual void RemoveItemsFromInventory(const TArray<FInventoryEntry>& inventoryChanges) override;
UFUNCTION(BlueprintCallable, Category = "Networked Inventory")
virtual FString ToString() const override;
protected:
// Called when the game starts
virtual void BeginPlay() override;
};
// Some definitions from the .cpp file
UInventoryComponent::UInventoryComponent()
{
// No need to tick.
PrimaryComponentTick.bCanEverTick = false;
mInventory = CreateDefaultSubobject<UInventory>(TEXT("Inventory"));
}
void UInventoryComponent::GetLifetimeReplicatedProps(TArray <FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(UInventoryComponent, mInventory);
}
bool UInventoryComponent::ReplicateSubobjects(UActorChannel* Channel, FOutBunch* Bunch, FReplicationFlags* RepFlags)
{
bool wroteToChannel = Super::ReplicateSubobjects(Channel, Bunch, RepFlags);
//const int32 inventoryObjectID = 0;
//if (Channel->KeyNeedsToReplicate(inventoryObjectID, ???RepKey))
//{
wroteToChannel |= Channel->ReplicateSubobject(mInventory, *Bunch, *RepFlags);
//}
return wroteToChannel;
}