How do you replicate a uobject?

I’m using structures for an inventory system, but you can’t pass a structure by pointer. This means I can’t have an array of derived structures as the inventory because, well, structures can only be passed by value (unless you don’t use the UPROPERTY marco, which I need for replication).

Because of this, I created uobject wrapper classes for the structures which can be passed by pointer. But I know uobjects aren’t automatically set up for replication.

This is what my uobject looks like:

UCLASS(BlueprintType)
class UItem : public UObject
{
	GENERATED_BODY()
	
public:

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Replicated)
	FItemStruct Item;
	
	
};

How do I set up replication for my wrapper class so my structure is flagged for replication? If it matters, besides the structure and uobject definitions, all other development will be done in blueprints (unless I run into more functionality problems in BPs).

1 Like

Try an AInfo class, maybe.

AInfo

I’m not asking what type of class to use. I’ve already chosen a uobject. I need to know how to set up replication for one because, from what I’ve found, documentation on adding replication to a uobject does not exist. Thanks.

To replicate UObject, instance must have owner (AActor), who can replicate this.

AActor must have next functions (You can use AGameState for storing items)

virtual bool ReplicateSubobjects(class UActorChannel *Channel, class FOutBunch *Bunch, FReplicationFlags *RepFlags) override;
virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;

And your object must implement next methods:

virtual void GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const override;
virtual bool IsSupportedForNetworking() const override { return true; }
virtual bool ReplicateSubobjects(class UActorChannel *Channel, class FOutBunch *Bunch, FReplicationFlags *RepFlags) override;
2 Likes

Thanks for the help! I think I implemented IsSupportedForNetworking and GetLifetimeReplicatedProps properly, but when I try to override ReplicatedSubobjects I get an error saying, “method with override specifier ‘override’ did not override any base class methods”.

My code:

bool UItem::IsSupportedForNetworking() const 
{
	return true;
}

void UItem::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
	Super::GetLifetimeReplicatedProps(OutLifetimeProps);

	DOREPLIFETIME(UItem, Item);
}

bool UItem::ReplicateSubobjects(class UActorChannel *Channel, class FOutBunch *Bunch, FReplicationFlags *RepFlags)
{

}

Thanks for the help.

edit: It seems here that the method ReplicateSubobjects exists in AActor. Did you mean to override a different method?

edit 2: So it seems like you don’t use the override keyword because its the first declaration of the method. But now I don’t know what to put in the function to replicate the item structure.

1 Like

Oh sorry, ReplicateSubobjects not must be overriden in UItem.

In best case you must inherit your object from AActor (AItem), not from UObject ( link ).

After that your instance will be network supported ( use DOREPLIFETIME in AItem::GetLifetimeReplicatedProps and AYourGameState::GetLifetimeReplicatedProps for your AItem array ).
Also you must set the variable bReplicates = true; in AItem constructor.

It seems here they replicated uobjects - you just need to manually do it. I really want to use uobjects instead of actors in this case. So I need to find out what to put in ReplicateSubobjects to replicate a structure.

Okay, you can use UObjects not AActors.
In your owner class implementation of ReplicateSubobjects must be looks like this:

bool AMyGameMode::ReplicateSubobjects(class UActorChannel *Channel, class FOutBunch *Bunch, FReplicationFlags *RepFlags)
{
    bool WroteSomething = Super::ReplicateSubobjects(Channel, Bunch, RepFlags);
	
	
	for (auto& item : Items)
		WroteSomething |= Channel->ReplicateSubobject(item, *Bunch, *RepFlags);
 
    return WroteSomething;
}

Replicate every item individually

That code would be in my uobject, right? If so, the uobject only has 1 item structure in it. Item is not an array. My uobject is a wrapper class for one struct. My array of items will be in an actor component.

When I try to do "bool WroteSomething = Super::ReplicateSubobjects(Channel, Bunch, RepFlags);
" it says, “UObject has no member ReplicateSubobjects.”

That code would be in your owner class of your uobjects. And in your uobject you must use GetLifetimeReplicatedProps to replicate inner structures.

1 Like

How would I do that in blueprints? My UItem is in C++ but my actor component, which is the actual inventory system, is written in blueprints.

And by ‘owner’ does that mean whatever reference I put into the “outer” pin in the node Construct Item? So in the picture below the owner of the item would be my inventory system actor component?

1 Like