Correctly set an actor reference from the level blueprint.

(Former Unity user here since their monetization “adventures”)

Hi all!
On a tile-based fighting game, I’ve done a significant amount of work in Unity. I’m looking to port most of this into Unreal.
The player operates on a manager authoritative architecture, meaning the player has to ask the Grid Manager to move him. He cannot move by himself.

In Unity, I achieved this by creating a Game Manager instance that would implement a “GetCurrentGridManager()” function. With a serialized field, I gave the player the Game Manager and he would call the function and get back the Grid Manager.

In Unreal, PlayerStart does not allow me to give a reference to my GridManager Actor before the player pawn is instantiated.

The ultimate goal is to have an AGridManager* reference in my Pawn so I can call GridManager’s functions like RequestMove() from my Pawn.

Through Blueprints, I’ve tried to store the GridManager reference in the game state, set its value from the level blueprint, then have the player pawn get it back again from the game state. However, all casts fail and the engine crashes.

How can I fix this ?

(Using the Pawn’s construction script won’t work either. I would also like to avoid using GetAllActorsOfClass since I will have A LOT of managers to reference later on)


1 Like

The most direct way for a new player to get a grid manager reference ( assuming the grid manager is an actor in the level ) is

image

I just read ‘avoid using GetAllActorsOfClass’, how many grid managers will you have!? :slight_smile:

If you’re using the level BP method, this

image

IS already the reference.

1 Like

It’s kinda hard to tell but I’ve read everywhere that using this « Get Actor of Class » multiple time may be bad for performance. I’ll for sure have more than one manager. This is a 3v3 game, if 6 players use this method to get a reference to a manager, game performance may be compromised.

Through the blueprint, Does setting a UPROPERTY() variable, from the player pawn, to have GridManager_BP (The one in the level blueprint) as a value allow me to call any function the GridManager class has ? Or do I need to expose any GridManager class function with the UFUNCTION() tag?

As long as you set whatever actor you retrieved as a variable, it won’t tax memory.

Not at all. The bad rap ‘get actor’ has is purely from people using it in a loop every frame :smiley:

Anyway, as @Leomerya12 says, you only have to do it once, then put it in a variable.

allow me to call any function the GridManager class has ?

Yes. You can call them from the blueprint reference.

2 Likes

Create Actor “GridManager”. And then you can either reference that actor and call event in it. Or other way around GridManager finds player controller and calls events there.

Do not use level blueprint if you are not really forced to do it because:

  • loading order, from level blueprint you are never sure if other actors are loaded
  • not all can be referenced, not all nodes can be used in it
  • every new level and you need to make new blueprint script inside
  • using single actor that keeps all code for some functionality is easier to maintain later

Also with actors you can create dispatchers, add blueprint interfaces, inheritance, CPP base code class etc.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.