Eternal apex // ambient cyberpunk megastructure endless rider

How to spawn and endlessly reuse/reconfigure the same set of road segments, to avoid the overhead/garbage collection/memory leaks of endlessly creating/destroying identical modular child actor components uncountable thousands of times (in the case of running the game at high speed for hours).

  1. Each road segment is an Actor blueprint named BP_Tunnel. This blueprint doesn’t have any logic, it only defines separate component meshes for the road, walls, ceiling, lights, etc, plus a point light emitter on the ceiling, and a trigger volume (highlighted here in yellow) that the player bike is guaranteed to pass through:

Each segment is made up of separate component parts to allow different predefined configurations, which are selected each time a segment is reused by hiding/showing the necessary pieces:

  1. There’s an Actor blueprint which controls all the road spawning/reuse logic called BP_WorldSpawn. This is dropped into the level where the spawning should begin (at the same coordinates as the player pawn begins). On BeginPlay, BP_WorldSpawn gets the player pawn start location, and determines how much space needs to be filled with segments based on how long each segment is: starting a short distance behind the player (SpawnTunnelDistanceBack, about 3 segments in this case) to allow for a following camera, and going forward until a predefined view distance is reached (SpawnTunnelDistanceForward, based on how far the camera can see forward due to fog, about 80 segments in this case for a very long view distance, but this can be scaled down for lower spec PCs while simply moving the fog “cutoff” distance closer by adjusting density).

  1. Next (still in BP_WorldSpawn’s BeginPlay event), a while loop tests if we’ve reached the end of the spawn range: if not, spawn a BP_Tunnel as a Child Actor component in the current spawn location, add the new child actor component to an array (TunnelsSpawned), set the new component’s tunnel configuration (does this section have a ceiling? walls? outer supports?), and increment the current spawn location by the length of one segment. Then repeat the while loop until limit is reached.

  1. Now on Tick in BP_WorldSpawn, get current player Pawn location, and see if it has gone far enough to need a new road segment moved out to the front of the line (if Pawn location + SpawnTunnelDistanceForward is greater than the location of the most recently spawned/moved segment + the length of one segment).

  1. If a segment movement is required, update SpawnTunnelLocationStart and SpawnTunnelLocationLimit to the new desired spawn range beginning and end (just like we did in BP_WorldSpawn’s BeginPlay before initially spawning the child actor components).

  1. Then loop through tunnel components to see which ones are outside (behind) our spawn range:

  1. If a tunnel segment is behind the player, set new spawn location (most recent spawn location + segment length), move the child actor component to this new location, and update its configuration if needed (walls, ceiling, etc):

The only caveat involves a World Origin Shift: when this occurs, the tunnel child actor components will need to be looped through and moved by the amount of the shift (as shown in step 6 of the previous post).

Hope this helps!

2 Likes