Associating a level-specific Actor with a spawned Actor

Suppose you want to make a rail shooter. Each level has a single “Rail” actor responsible for defining a track through the level. You want every player who spawns in the level (PlayerController or Pawn) to have a Blueprint variable with a reference to the Rail. How can this be done?

The problem is the Rail is created by the level editor, but Players and Pawns are created dynamically. As far as I can tell, there’s no way to set a level-specific “global” that other blueprints can access, nor is there any “player spawned” event that placed Actors can respond to.

The only way I can think of to get the two communicating is to put a trigger at the player spawn that sets up the association, but I can’t help but think there’s a better way.

The best solution I’ve found so far is it store references to my level-wide global Actors in the GameMode in response to their Begin Play events. The spawned Actors can then retrieve them in their Begin Play events. GameState would work too, but that’s more for player scores and such than level-specific wiring.

Hi Dostende,

You can add a rail reference variable to each blueprint that needs to know it and then in the variable’s details, check the “Expose on Spawn” option. Now, wherever you spawn that variable you’ll have that as a separate input on the spawn node. So whatever is spawning the pawns can pass along its reference to the rail.

If the spawner is already placed in the level, then you can assign it a reference to the rail by exposing its rail variable with the “Editable” checkbox, or you can assign it a rail reference in the level blueprint with the BeginPlay event.

I don’t know your specific use case, but hopefully this shows you ways that you can shuttle your “Rail” reference around so everybody has access to it.

Cheers - MikeB

That sounds like a great solution but as far as I can tell, GameModes are responsible for the default player spawning logic and there’s no way to override that behavior in Blueprints, nor are any associated events generated. I know it’s possible in C++, but everything’s possible in C++!