Client RPC handles earlier than BeginPlay[updated]

I got strange behavior in my class. I spawn actor instance of my class on server by calling ServerRPC function from client. Server spawns actor with client as owner. Next, Server calls ClientRPC on spawned actor (need to say that actor have bReplicates = true; and bAlwaysRelevant = true; in constructor).

So actor is replicated to client and there is called ClientRPC_Implementation. First time all works fine, but that’s all. Second and next actors wont properly execute ClientRPC (because there is condition with IsValid() check, that checks property, which would be set in BeginPlay).

I attached VS to game instance and found that for some reasons ClientRPC on client-side is called earlier than BeginPlay(on 2nd+ spawned actor), but first spawned actor have done work properly.

Anybody can reproduce this behavior? Or maybe I missed something then give me right point to see. Thanks in advance

UPDATE:
Okay, as nobody answers I made little sample(in 4.11.p3 already). That’s log which I receive:

LogTemp:Warning: Within ADerivedOneActor::BeginPlay()
LogTemp:Warning: Within ADerivedOneActor::ClientRPC_Implementation()
LogTemp:Warning: Within ADerivedTwoActor::BeginPlay()
LogTemp:Warning: Within ADerivedTwoActor::ClientRPC_Implementation()
LogTemp:Warning: Within ADerivedOneActor::ClientRPC_Implementation()
LogTemp:Warning: Within ADerivedOneActor::BeginPlay()
LogTemp:Warning: Within ADerivedTwoActor::ClientRPC_Implementation()
LogTemp:Warning: Within ADerivedTwoActor::BeginPlay()
LogTemp:Warning: Within ADerivedOneActor::ClientRPC_Implementation()
LogTemp:Warning: Within ADerivedOneActor::BeginPlay()
LogTemp:Warning: Within ADerivedTwoActor::ClientRPC_Implementation()
LogTemp:Warning: Within ADerivedTwoActor::BeginPlay()

That log I received on both: client and server instance(here in PIE, in my other project I received this behavior in standalone application). As you can see from 5th line, Client_RPC began occur earlier than BeginPlay(be care here are 2 different actors that writes to log)

Here’s source files for project. If someone says how to upload whole project that it will take less than 5 MB then I can upload whole project.

It seems for me like a bug. Now can anybody give me some kind of answer, is it right flow of spawning and replicating actor or not?

I have 2 classes that are working the way above, and within 1 game session first actor of each class is executed properly and next instances not. If I disconnect from server(with changing map to main menu) and connect again, then again: first try - success, next tries: fail.

Can I drop out Binaries and Internediate folders from archive or it will be not open?

Hey alkohol-

If you zip the full sample project you should be able to upload the zip file to dropbox, then you would just need to send the link to download the project directly. You can post the link here or send me a PM on the forums for privacy.

Cheers

Binaries, Intermediate, and Saved folders should all be safe to remove before zipping to reduce compression size, yes.

Sorry for late answer, . Here is the project.

Hey alkohol-

We are still investigating the project you sent to try to understand why the ClientRPC() function is printing before the BeginPlay() function. I will update you here when there is more information.

Cheers

Hi !

Updated link, it will expire after 2 days, so if it’s needed I can reupload it again.

and again…
Thanks, for supporting :slight_smile:

Hey alkohol-

After further investigation I have entered a bug report (UE-26811) to investigate why the ClientRPC function is being called before BeginPlay.

Cheers

Thank you, .

It’s pretty old thread, but is there any update for this issue?

Unreal Engine Issues and Bug Tracker (UE-26811) says that the problem cannot be reproduced. But I also suffer this type of problem now on version 4.19 (with very small customization)

The situation that I saw is little different with yours. The Multicast RPC was handled after the Actor’s BeginPlay but before UActorComponent’s BeginPlays.

I also spawn the actor at runtime, and Multicast RPC is called from dedicated server after player joined.

If there are documents about Actor and Components lifecycle with replication behaviors, please let me know.

Thanks!

So I was having a similar problem, and I think it’s a bug in DataChannel.cpp. UActorChannel::ProcessBunch will spawn the actor if needed, dispatch all available bunches, and then call BeginPlay at the very end of the function via PostNetInit.

It should really be calling PostNetInit immediately after any Initial bunches have been dispatched and before any further bunches are processed, it also looks like it’s passing in RepFlags.bNetInitial = true; for all bunches in that loop, I’m not sure if that’s okay or not?

I tried moving the init call to inside the loop at the end, but that assumes that there’s always exactly one initial replication bunch.

Edit: This was a bit of a necropost so I created a new question on UDN.

I am experiencing the exact same on my project. 1.) I spawn an actor on the server in a blueprint. 2.) Immediately after, i call a multicast RPC from blueprint. The RPC itself is implemented in C++.

First actor: Actor gets replicated, BeginPlay executes, RPC executes.

Second actor: Actor gets replicated, RPC executes, crash because RPC uses data initialized in BeginEvent.

Time between spawning of actors doesn’t matters. Happens 100% of tries.

I worked around it by having a BeginPlayCalled property, which is set to true at the end of BeginPlay and all client RPC’s check for this variable and if false, add the RPC to a event buffer and return out. The event buffer is executed at the end of BeginPlay. This fixes it reliably, but is quite some extra work.

I need some time to isolate code to have a project to replicate this behavior. Just a heads up that thiis problem is still existing as of 4.21.1 and “quick” workaround to get your project to work in multiplayer.