Download

bReplicates Resets to False after Construction

I’m trying to implement a player inventory system in a networked game. I’m using a UActorComponent to do the inventory system and I’m having some issues with replication.

following this official unreal guide I have something like this (Note the log statements):


class AMyCharacter : public ACharacter {

    AMyCharacter() {

        ...

        Inventory = CreateDefaultSubobject<UInventoryComponent>(TEXT("Inventory"));

        if (Inventory) {

            Inventory->SetIsReplicated(true);

        }

        // Prints 1 (true) on server AND client

        UE_LOG(LogTemp, Warning, TEXT("Inventory replication %d"), Inventory->GetIsReplicated());

    }

    void BeginPlay() {

        // Prints 0 (false) on server AND client

        UE_LOG(LogTemp, Warning, TEXT("Inventory replication %d"), Inventory->GetIsReplicated());

    }

    ...

    private:

    UPROPERTY(Replicated)

    UInventoryComponent* Inventory;

};

class GAME_API UInventoryComponent : public UActorComponent {

    ...

    private:

    UPROPERTY(Replicated)

    uint16 Capacity;

    ...

};

The problem is that the Inventory member variables don’t replicate. I was able to narrow the problem down a little by putting those print statements in. As you can see GetIsReplicated() returns false after the constructor. Why does this happen?

I have also tried setting SetIsReplicated and/or SetIsReplicatedByDefault to true in Inventory constructor and that didn’t work.

Another piece of information that could be useful is that I believe inventory replication was working perfectly fine with this exact code (I use version control) and this is the second time such thing happens. The first time it magically fixed itself :slight_smile:

I’ve found a workaround since but I would like to know why this is happening. The workaround is to call SetIsReplicated(true) in BeginPlay() of character class on the server