Reference to spawned objects

Hello There,

I am building a Digital Twin of an airport and am using a level BluePrint to retrieve aircraft positions at the airport every few seconds. The blueprint class spawns the aircraft and gives variables (like position, speed etc) to the spawned object.

What I can not get is how to reference to the spawn objects, as after a few seconds I must update the moving aircraft with a new position (so that I can transform lerp to the new position). It seems that I cannot use the “getActorfromClass” as the list of spawned objects is changing (aircraft fly out of the airport and need to be destroyed, new aircraft are coming is and need to be spawned).

In Unity (build same functionality) I could have a unique reference to the spawned object, but I can not find the same functionality in Unreal (and the functionality must be there).

Someone has an Idea how to do this via BluePrints?

Thanks in advance,

Peter
KLM Royal Dutch Airlines

Hi Peter, welcome to the forums. Why would you need to do something like GetActorFromClass? If you spawn an aircraft in the Level BP, this already returns a reference. You can keep for example an array of Aircraft References, adding new aircrafts to this array, removing those, that are destroyed.

Instead of handling things in the level BP, you also can create a separate kind of Manager Class for this, which handles all the spawning and movement of airplanes. Creating such a class as an actor, that can be placed into the level makes it easy to access objects from other blueprints as well.

1 Like

Hello herb64, Thanks for your answer.

There is one difficulty with your answer.

If I store the reference (aircraft123) in the array, then I have lost which aircraft (aircraft registration ID) is belonging to the spawned aircraft123.

If I store the aircraft registration ID in the array, then I have to getAllActors, to find the reference based on the aircraft Registration ID field for every update cycle (resource heavy if I read the documentation).

It seems that I cannot use an array with a key (spawn reference) and a value (aircraft Registration ID) so that I can find the reference based on Aircraft Registration ID, and do the logic to either spawn or update based on this reference.

Yep, that’s something that came into my mind as well. Already wanted to send an update. You are right, doing a GetActorByClass is not a good option if done too often like on every tick. You can use a Map instead of an Array, having an Aircraft ID as key and aircraft data as value (value can be a struct as well, so you can store more information as you like). Having this in a kind of manager Actor - you simply can write functions to query and set aircraft infos based on that key.

1 Like

I am also looking into using an actor Tag as a possible solution, like:

Sure, you could add the Aircraft ID as a tag as well, but this would require to use GetActorByTag - this also is a quite slow operation.
If you need to lookup Flight IDs, the map (it’s an equivalent to a Python Dictionary, in case you are familiar with python) is a better way.

1 Like

Hi, here’s some example: create a bp for the AirCraft Manager:
Add a billboard, which is shown in the level, when you add it (adding one manager only, not checking here, that only one is present, but should be done of course)

This has the AirCrafts Map as variable, which has a string with Flight ID as key, and a reference to the AirCraft actor. In the example, it has 3 functions only:
Add Airplane, Set and Get Pilot Name just for testing…

The aircraft itself is a blueprint as well, in my case this has no mesh, just a name of pilot as Text Variable. (Text is preferred type for all stuff, that should be visible to the player)

For testing, I added the following to my player character.

On Beginplay, just getting a reference to the AircraftManager, that has to be dragged into the level before - you can adjust the billboard with your custom one, here using the default one.

grafik

Now for BeginPlay: here doing the GetAllActorsOfClass only once and store a reference for the manager. Add 2 airplanes with IDs and set pilot names…

For test: On pressing K, using the manager to query information about the Aircrafts, using the Flight ID keys:

No checking done, all hardcoded, only limited functions, but it should make clear the idea.

I hope this is helpful to you

2 Likes

Many thanks, great help!! :grinning:

I have implemented it, and it works!

1 Like