Replicated actor passed to a multicast RPC function is null on clients

Hi all,

Seeing a strange issue that I think may be to do with actor replication over a network.

In this scenario, clients will equip items - this involves making a call to the server to spawn the item, a pointer to that item object is then passed to a multicast function so that clients may set variables that are relevant to them based on the actor in question.

Unfortunately, after the actor has been spawned, and a point to it is passed as a parameter to the multicast function, it comes out as NULL on the client side.

Here is a quick code snippet:



//snippet from the server rpc function
    AItemActor* itemActor = ((UOlusiaGameInstance*)(character->GetGameInstance()))->ActorManager->SpawnActorFromBlueprint(character->GetWorld(), deserializedItem->ActorBlueprint);

    itemActor->SetReplicates(true);
    itemActor->UpdateAllReplicatedComponents();

    MulticastEquipItem(character, location, itemData, itemActor);



The first line is quite verbose but essentially it just spawns an actor in the world using UWorld->SpawnActor<AItemActor>.

The last line then just calls the multicast RPC function from the server - but when I step through, itemActor comes out as a null pointer.

Is this to do with the item not yet being replicated at this point? Is there another way to resolve this issue, perhaps by making the replication instant?

The actor probably hasn’t fully replicated yet, until it does the client won’t have created it and the engine won’t be able to turn the network ID into a valid pointer. Additionally, the actor may not even be relevant for some clients.

You can’t spawn something then immediately call an RPC related to it as you have no control over which will arrive first - and this behaviour can be pretty random each time. Some people would encourage using a delay, but that an awful bug-prone solution to something that honestly just needs a slight redesign.

Depending on what you need to do, I would just override the BeginPlay() function of the spawned item and perform whatever setup you need to right there. That way you have no race conditions and as a bonus, you avoid the overhead of a multicast altogether. As soon as the item arrives on each client, they will perform whatever setup they need to.

8 Likes

Thank you.It worked for me

Thank you very much, TheJamsh!

your answers help a lot both on the forum and on the hub in the difficult study of multiplayer mechanics.

1 Like