we have a main blueprint (tank) and a child actor component with a sub-blueprint (weapon).
On Begin Play of the tank we bind to an event of the child-actor and noticed that this only executes correctly on the server, even though the event is triggered on client and server.
It seems that at begin play the instance of the child actor only exists on the server and will take a moment to replicate itself to the clients. Putting a delay before binding the event in the tank will make the event trigger correctly on all machines.
Is there any function or event that is called when the child actors are correctly replicated and / or initialized so that we can have a more reliable way to bind an event to them other than a delay, which is kinda wonky?
(This is blueprints but a C++ solution will be fine, too)
I think I would make a boolean variable like “IsWeaponReady” on the tank and when the weapon is ready, it can get the parent tank and set the variable to true?
The solution entails not using ChildActorComponents first of all. They are broken.
What you should do instead is use AttachToActor to attach the weapon to the tank server-side (you can do that in PostInitializeComponents or BeginPlay for example). The weapon should be a RepNotify (ReplicatedUsing in C++) property (also the weapon actor should be replicated of course) and when the OnRep function fires then you are sure that weapon actor has replicated.
Why this happens? Why on BeginPlay I can’t guarantee that my weapon actor has replicated? Reason being is that weapon and tank are different actors, and each has its own NetUpdateFrequency, so they replicate at their own rate. That’s why OnRep_PtrToActor() functions are so handy in multiplayer.
They are somehow workable (still buggy) for single player, but for multiplayer they are terrible. I vaguely remember actor not being spawned/destroyed correctly, and editor crashing because of some CDO changes. For engine versions, they are still buggy to this day, and would bet that was always the case. The best practices that involves them, is to avoid them.
If you would like to hear more, then looking for “CAC” in Unreal Slackers discord server search tool is your best bet.
Thanks for the elaboration! I must say that except for the original question they have been working well in multiplayer for us so far (5.0.3), we just recently made a multiplayer test with about 8 people and the weapons (ChildActorComponents) worked fine, no desync issues.