0
\$\begingroup\$

I'd like to know if there is a way to get a reference to the instance of an Actor (written in c++) in the scene from a blueprint even if it's not in the scene, or at least without making a new one using that Actor as base class.

I know there's a method to get all the objects in the scene, but it wouldn't be that cool to iterate over each one of them, so i absolutely want to avoid something like this.

Basically, i have a class called ABoard, that inherits from an AActor where i got some methods to deal with the board itself. Also i have some blueprints that will show the MainMenu, and someother stuff, and what i want to do, is to call a method in the ABoard from one of these blueprints to be able to set up some stuff, like: "Play as single Player, whatever it is". But, i cannot do this because (of course) the blueprint does not have a reference to the object/instance in the scene i'm talking about.

Since i'm relatively new to Unreal Engine, i have no idea how to solve this kind of problem.

\$\endgroup\$
2
\$\begingroup\$

If you cannot simply give the Blueprint that needs the actor a reference to that actor when it is constructed or invoked, then rather than search for the actor, you should simply save a reference to it (when you create it) someplace you can easily get to from the Blueprint.

You can create a property on some other well-known, accessible object to do this. Good candidates for this are a custom UGameInstance subclass or game state or perhaps a level Blueprint. Using a game instance, for example:

UCLASS(Blueprintable)
class UCustomGameInstance : UGameInstance {
...
public:
  UPROPERTY(BlueprintReadWrite)
  ABoard* MyBoard;
...
};

Once you create and hook up a custom game instance, you can recover it with the GetGameInstance node in Blueprints, cast it to your custom type, and then access the MyBoard property, allowing you to cache the board actor when you create it and refer to it later easily.

If you must, such as if the actor is hand-placed in the editor and you need to find that instance, Unreal does provide mechanisms to find objects at runtime, such as TActorIterator and various Blueprint-exposed helpers. Actors aren't indexed for efficient runtime lookup by name, however, so any such method will necessarily involve iterating linearly over at least some subset of the entire actor list until you find one with matching criteria. This is not very efficient; if you must find an object this way, you should try to perform such a lookup as infrequently as possible and cache the result in a property (as above) anyway. Or restructure so that you can spawn this actor at runtime for easy caching, possibly by placing a proxy actor instead and swapping that out with the real one during BeginPlay or similar.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.