I have a question about using references in Unreal C++.
I have an ActorComponent which is used by multiple actors. The ActorComponent acts on data (TArray) which is meant to be replicated, but the type of replication differs depending on the Actor. Some Actors want to replicate the data to all, and others only want to replicate using COND_OwnerOnly.
With that in mind, I have the data itself stored on the Actors so they can specify the type of replication in their AMyActor::GetLifetimeReplicatedProps() functions. Now different Actors can replicate their TArray data in different ways depending on their needs.
The only problem left is allowing the ActorComponent to access the data (TArray) stored in their owning Actor classes. This sounds like a good place where a reference would come in handy. If the ActorComponent had a TArray<>& member, the problem would be solved if I could initialize the reference in the ActorComponent constructor. But based on what I have seen so far, it doesn’t seem possible to use references in this way with Unreal.
The temporary solution that I have come up with is for the ActorComponent to have a member:
and then initialize it in ActorComponent::InitializeComponent() or ActorComponent::BeginPlay() by using GetOwner() to get access to the owning Actor’s TArray. This should work but it is a bit ugly to work with… now accessing elements in the TArray looks something like this:
Also it makes more sense to use a reference here since the ActorComponent shouldn’t be able to function without the TArray data. A null Data pointer doesn’t make sense because the component needs the data to work properly.
Is there any way to solve this problem using references?
As far as I know, it’s not possible to have the TArray be a member of the ActorComponent with my requirement that different actors be able to replicate the data in different ways.