Should actors register with GameMode or GameMode search actors?

This is a very specific question, and I’m sure the main answer is YMMV, but:

As a general good practice, should I have my actors call to GameMode and register themselves or should I have the GameMode lookup the Actors?

Hi HeavyStorm

It really depends on the projects requirements. As a good practice, actors should register themselves with the Game Mode for two main reasons:

  1. It stops the Game Mode from needing to use functions “GetAllActorsOfClass” as it’s slow and expensive.

  2. Actors load in the world at different times. So if the GameMode attempts to find them at the start, they may not be valid yet. So having the actors register themselves with the game mode when they are ready in the world will ensure the Game Mode will receive valid references.

However, as I said, it can depend on the projects requirements.

Hope this helps.

Alex

I believe UE4 loads the GameMode quite early on, before BeginPlay() is called on any actors. It’s also generally quite easy for an actor to get access to the GameMode. Thus, having actors register with the GameMode is probably the least costly method to implement. Further, if you have the possibility of more actors getting spawned or loaded during gameplay, they can quickly add themselves to the GameMode easily.

That said, if you’re dealing with a class that you didn’t write and don’t want to subclass and there aren’t any more of them being added at runtime, having the GameMode go and cache them all one time at startup certainly isn’t a big deal.