How to run Unreal blueprint nodes on BeginPlay that depend on another blueprint UE4-27

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:

  1. 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.

  2. 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?

1 Like

You can delay the beginplay of Blueprint A.

Simply try to get the variable of B, and validate it with the “is Valid?” Node.
This node has two outputs… Valid and not valid.

If valid, go on with whatever you wanna do with B’s Variable.

If not valid, add a delay node with… Lets say… 0.001 seconds. And right after the delay drag your execution line back to the beginning of begin play… So it will run the validation again… If not passed… Redo it… Etc…

Validation checking is your mightiest power and should be done whenever you try to get Stuff from extern BPs or if something requires specific Cars to be filled.

You even can checkout the validation of variables you drag & drop from the Variable list, by rightclicking the get Node and convert to validate check.

2 Likes

An alternative could be have A check B, if B is not ready, A binds to an event dispatcher that calls all binded when B is ready.

This. But just have A check first, then bind if fail.

3 Likes

Instead of running on A begins play, why dont you just make it run as a different A’s function called from B’s when is the right time? (like B begin play)

You can change which part of the frame your code runs in. Typically, it’s pre-physics

but you can run in other parts of the frame

image

Try putting the thing that’s starting to soon further down the list.

I may have to re-use this pattern many times throughout the game. Will this approach slow things down by a lot? I’ve heard Event Dispatchers are relatively expensive processing-wise.

I think this is the best approach. The delay in the loop still feels hack-y IMO but it does what I need it to.