Construction Scripts runs in random order and sometimes twice

Scene has two blueprints. Situated in air above ground.

On its construction script BlueprintA LineTraces to ground (or other objects) and snaps Spline to ground level.

BlueprintB takes BlueprintA’s Spline height at some point and aligns to that point.

Everything works perfectly until level is reloaded. On level load Construction Scripts run in RANDOM order.

And if BlueprintB’s construction script runs first, strange thing happens:

1.BlueprintB consctuction script runs, result unknown.
2.BlueprintA LineTraces and FINDS NO GROUND! It stays in air because nothing to align to.
3.BlueprintB aligns to air (wrong) position of BlueprintA.
4.BlueprintA LineTraces and FINDS GROUND! It snaps to ground.

Sometimes construction scripts run only once, then only two last steps run. Result is the same.

So, depending on random run order, BlueprintB may end up in air (wrong) or on the ground (correct).
BlueprintA always end up correctly on ground.
I figured out this order by PrintStrings, it’s not that obvious.

My questions are:

  • Why first time BlueprintA LineTraces it founds no objects beneath it? They aren’t still created? Is there a way to debug object creation order on level loading?

  • Why Construction Scripts run twice? It seems to not depend on content, it may be PrintString only.

  • How to fix this? I need these two Blueprints’ construction scripts run in correct order.

Only fixes I found is to reload level hoping that this time construction script will run in correct order (sometimes works) or manually updating all BlueprintsB after level opened.

You can’t configure the order. One way to do it is to have a 3rd BP that ( from it’s constructor ) calls editor level custom events in A and B, in the correct order.

Why first time BlueprintA LineTraces it founds no objects beneath it? They aren’t still created? Is there a way to debug object creation order on level loading?

  1. You can always use Delay and check if your build will work after 1 second. In principle, when loading a game, there is a process of compiling shaders and building a level at the moment when the player is looking at an interesting picture.
  2. Construction Scripts are more suitable for building on the level and checking in moments when the game is not running. I would suggest that you do everything in event graff for accurate observation and control.
  3. You can use Set Timer by Function name or simple delay.

Delay and Set Timer by Function will not help because they don’t work in Construction Script.
I don’t want to move everything to BeginPlay because BlueprintB must be located correctly in Editor. So it must be done in Construction Script.

I found workaround for my problem. I changed BlueprintA.

Now when BlueprintA LineTraces and founds objects underneath, it creates spline as usually, and then it stores every point coordinate of that spline in exposed array of vector variables.

When LineTraces fails and returns no ground underneath (it happens when level loads and BlueprintA spawns before other objects), it reads stored array and creates spline with points from stored coordinates.

BlueprintB works perfectly with no changes.

This is the only way I found to pass spline data from one construction script run to another.

Hope it will help someone.