I need blueprint A to run nodes in BeginPlay which rely on a variable in blueprint B, but that variable is null until set in B's BeginPlay function. Of course, A's BeginPlay could run before B's and I would run into errors. I can think of two ways to get around this, but neither feel like a proper approach:
In A's BeginPlay, add a Delay node with a second or less duration in the hopes that B's variable has been initialized by then. It seems like this could easy break things and isn't smooth.
Have an Event Dispatcher in B called "VariableSet". A binds an event to it in BeginPlay and that event runs the dependent code. This usually works but I haven't heard of anyone doing this.
Is there a proven, documented method to avoid null pointers in BeginPlay?
I've faced the similar problem in the past. And in my case I just used Event Dispatchers. I'm not too proud of that aproach but It did a job.

For me it worked really well because one Parent had a lot of Childs and these Childs had their own Childs, etc, etc... and due to Dispatchers and Events I was able to easily track what was going on.
What works for me is to use a structured hierarchy for setting up the environment.
This is the usual way of working, it is, for example, how ue works internally with its subsystems.
For example (this is not in order):
i have a game mode that is in charge of spawning, loading, initializing, and coordinating all subsystems and managers. It also exposes pointers and functions to obtain references to such things if needed.
it spawns everything that needs to be dynamically spawned.
wait for things to load.
waits for systems to initialize (for example savegames, or external data)
when all things are loaded and spawned. important actors and subsystems have a special function to run "init" code. e.g. getting variables for other subsystems.
then proceeds with starting whatever level.
this has been critical for having a deterministic and stable games on many projects over decades.
i have killed so many bugs with that.
and i've run into many situations where the order of the elements matter. and many others where timing was critical. For example when implementing a save system and a loading screen, i need to be able to control, coordinate, and wait on each.
or that i needed to add extra stages, for example, the "init" stage, that happens after begin play and after subsystems have been loaded and actors too.
again this is pretty normal in ue code, you can see the actor lifecycle and its several stages, and how ue coordinates subystems.
about hierarchy. in turn i have each actor/subsystem/manager, to be responsible for its dependencies. (e.g. components, children, and other objects). they receive the signals from the game mode and in turn control their own stuff. which is pretty basic oop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With