How to set up a reference to a static level object in a dynamically instantiated actor?
I have a fixed, static object in my level placed in the editor that I want my player-controlled pawns to be able to reference in their C++ code. I would like to set up that reference in the editor.
It doesn't seem possible to set up those references by deriving the pawn class in BP and setting the object reference as the default value. They just stay as None despite being highlighted when attempting to drag the object to the slot.
So how can I do that without fragile logic like finding the object by name?
asked Jun 11 '17 at 10:53 AM in C++ Programming
Well, if you know that the actor is a singleton, the usual way to do it is to create a public, static function in the singleton object's class which uses an actor iterator to look for an object of its own class and return itself. (See here: https://wiki.unrealengine.com/Iterators:_Object_%26_Actor_Iterators,_Optional_Class_Scope_For_Faster_Search#Actor_Iterator) You'll need to pass it a world so it knows where to look, though.
Does this thing have to be an actor, though? There are better ways of doing this in most cases, like with a game singleton. That's a better way to ensure the object exists without adding magic stuff your level designers need to add to every level just to get them to work.
Follow this question
Once you sign in you will be able to subscribe for any updates here