Replicate base class UPROPERTY in subclass only

What I am looking to do is to have a UPROPERTY that exists in a base class replicate (ReplicatedUsing specifically) ONLY for a subclass of that base class.

My use case is like this:

  1. Base class is a weapon and so far has had no need to replicate its target to the client yet. This base class had the Target variable.

  2. Subclass is a specific type of weapon that now needs to replicate its target to the client.

  3. I do NOT want the base class to replicate its target as an optimization. I don’t want the weapons in the game replicating the target unless it has to.

Is there anyway to accomplish this?

I don’t want to sound mean but what you want to do makes no sense. Weapons are just props.

You already have a pawn that has the weapon equipped and the pawn is replicated. You should keep the current target in the pawn.

What you don’t want to do is add more replicated actors. That adds unnecessary overhead.

For a weapon, you want to equip it on the server and use a RepNotify or RPC to tell the clients what the current weapon is.

The only time a weapon should be replicated is when it is a pickup – and in that case it’s usually a pickup class that uses the weapon mesh. And a pickup actor is not equipped by the pawn, its information is used to tell the pawn what weapon to spawn and equip.

This way, the server and each client all spawn their own weapon instances. You don’t replicate the weapon itself. And all the replication happens in the pawn where it belongs.

Think you may have misunderstood what i’m asking.

Lets say Class A has a variable:
AActor* TargetActor;

I then inherit from Class B and want TargetActor to replicate ONLY for class B.

To make it replicate we then do this;

UPROPERTY(ReplicatedUsing = OnRep_SetTargetActor)
AActor* TargetActor;

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

	DOREPLIFETIME(ClassA, TargetActor);
}

This will work, but it means all instances of Class A will replicate TargetActor as well which is not needed. Just the subclass, ClassB cares about needing the TargetActor reference to be replicated for it.

So to reword my question: Is there a way for a subclass to replicate a variable existing in its parent class, without the parent class also replicating that variable?

A work around is to have a new AActor* ClassBTargetActor reference in ClassB setup to replicate and have ClassB setit whenever TargetActor is set. I was just wondering if there is anyway around that.

Oh, I guess “weapon” and “target” threw me. That’s an interesting request.

You should check out the AActor::SetReplicates() function. That will turn replication on and off.

I would try having the base class variable replicated (with the GetLifetimeReplicatedProps entry) and turning it off in the BeginPlay (or perhaps the constructor would work too). Then in the base classes that need replication, just turn it back on.

Another option comes to mind but I’m not sure it will work. You could try having the base class variable not replicated, then turn replication on in the derived class and put the entry in that GetLifetimeReplicatedProps. It makes sense in my head but I’m not sure the engine works that way. You could experiment – because I’m not sure if the class that declares the variable also has to have the lifetime-rep function entry. If not, this method should work.

Or a variation: not replicated in the base class but the lifetime-rep entry is there anyway. Then the derived class turns replication on.

I’ve never tried this so it’s up to testing. But the first option would work for sure.