When can Actors exist between levels?

A developer was asking about the relationship between Actors and Levels, and if actors can exist between levels.

In my understanding, Actors always belong to a specific level. You create them by calling UWorld::SpawnActor and they are destroyed when the level is destroyed.

But the developer was curious about how things work when transitioning between levels for example. I’m guessing that depending on the way you transition between levels, the actors are destroyed and re-created (traditional Open Level change), or the ownership of existing actors (player or enemy characters, existing effects, etc) are perhaps transferred to the new level (seamless travel?).

Also they mentioned the root set for garbage collection, and was wondering what happens if an Actor belongs to that when you change levels.

Reading the Actor Lifecycle documentation, it sounds like they will always be destroyed if the level they belong to is unloaded:

Coming to the End of Life Actors can be destroyed in a number of ways,
but the way they end their existence
is always the same.

During Gameplay These are completely optional, as many Actors
will not actually die during play.

Destroy - is called manually by game any time an Actor is meant to be
removed, but gameplay is still
occurring. The Actor is marked pending
kill and removed from Level’s array of
Actors.

EndPlay - Called in several places to guarantee the life of the Actor is
coming to an end. During play, Destroy
will fire this, as well Level
Transitions, and if a streaming level
containing the Actor is unloaded. All
the places EndPlay is called from:

  • Explicit call to Destroy
  • Play in Editor Ended
  • Level Transition (seamless travel or load map)
  • A streaming level containing the Actor is unloaded
  • The lifetime of the Actor has expired
  • Application shut down (All Actors are Destroyed)

Regardless of how this happens, the
Actor will be marked RF_PendingKill so
during the next garbage collection
cycle it will be deallocated.

In general I’m not sure it’s a good idea to design your game intentionally in a way that Actors exist between levels (unless you’re just directly using existing engine features that may do this like seamless travel), but they want to know the general rules of the system so I wanted to confirm my understanding of them.

Thanks!

Hi Joe,

There is a forum post on how to keep actors between levels. Keeping Actors Between Levels - C++ - Epic Developer Community Forums

You use seamless travel to move from map A to B (with a temporary map C in between to handle transition without going out of memory) You can add your own actors to persist into the new map if you wish by adding it to the seamless actor travel list.

Here is an excerpt from the function comment on SeamlessTravel:

“You may need to implement GameMode::GetSeamlessTravelActorList(), PlayerController::GetSeamlessTravelActorList(), GameMode::PostSeamlessTravel(), and/or GameMode::HandleSeamlessTravelPlayer() to handle preserving any information that should be maintained (player teams, etc) This codepath is designed for worlds that use little or no level streaming and GameModes where the game state is reset/reloaded when transitioning. (like UT)”

The way it is setup you can add actors that contain information that you need to preserve between levels. In many cases you can add bits of persistent information to a PlayerState or GameState instead unless you have specific reasons to use a new Actor to contain this information.

By default seamless travel persists the following objects:

  • GameState
  • GameSession
  • All PlayerStates (Get Reset() during the transition)
  • GameMode (until the final destination is loaded)
  • Each local PlayerController with HUD and PlayerCameraManager (will be replaced at the end of the transition)

Hope that helps clear things up a bit. Let me know if you need additional information.

,

Tom

Thanks Tom! That information is super helpful. I passed it along to the developer.