Download

Using blueprints to get all actors of class (That are NOT in the game yet)

So, As the title says, I am looking for a way to get all the actors of a class that are not yet spawned in the game.

I have a Master Class Blueprint that all my items are children of. However, Unreal Engine 4 only allows me to get an array of actors of class that are already in the persistent level. I’m wondering if there is any reasonable work around, other than spawning all the actors into the world in a “dead zone” and then just referencing them. That’s not ideally especially if players find a way to locate that dead zone and figure out a way to get to the items. I’d much rather keep those chances off the table overall. Please, any help would be appreciated, thanks!

Can you specify why you need to get all actors? You should be able to just cast to your parent blueprint to access what you need.

Yes of course, I am working on some administrator commands, so that admins can spawn items in to the game, or summon some creatures. However, My admins will only be able to spawn the items that are already in the game.

It is simple pointer to something not yet created in game does not exist, so unreal cannot give you array op pointer to all not existing actors. Instead you need to make such array.

So you kind of cannot create that array, there is also one more problem, if those chi;d pbjects are of different classes, you may have problems of keeping it all in single array.
Only simple solution i can think of is: making enum that has state for each class of child actor then using “switch on enum” and spawning correct actor on each enum stat.
Anything you try to automate here will make messy code. It is doable to force unreal to spawn every possible class from list but i think not worth hassle. Enums are easier, and later much easier to read and debug.

Well see, it’s a pretty simple function that I am actually surprised Epic Games doesn’t implement. I mean, at least give us the ability to specify in each Item Blueprint whether or not we want it to be added to an array or something so that when the game loads for the first time, it can populate a drop down menu of all the items the game has. For example, If I have a master Item actor blueprint, specify in that blueprint that I want this blueprint and any child of this blueprint to be added to some sort of array system or enumeration automatically, so that when the admins open a UMG widget their drop down list’s are populated with all respectful items. In my opinion based on Unity Game engine, this feature is not too difficult to implement and is something i’ll look into adding as a plugin for now but I feel Epic Games should take a look into this and add this feature or one similar to it. I don’t want to have to set up a data server just to implement actor blueprints into my game without spawning them first.

I have question: what you mean list populated with all respectful items. And more exactly what you mean by items? Are those actual allocated and created actor objects, pointers to some assets in game, classes of actors that you may create.

You can get list of all actors, almost everything is an actor in unreal, you can get class of actor (having pointer to actor). Not sure if you can get list of all classes used by unreal, so i think you need to have actor loaded into memory for it to be on any list. And unreal does not like loading unnecessary objects.

You could create blueprint interface, add it to classes of objects you want on list. Then get all actors with interface. But again if actor is not loaded it will be not on that list. “Get all actors” nodes return pointers to actual actors, and if those are not loaded they do not have pointers.

Why you need to code it all yourself:
Imagine you created fruit item, it has some standard value like mass, food value etc. All those fruit differ only by visuals that are: static mesh model. texture color, some dirt mask. Should every possible combination of fruit actor be on that list or just one actor with first model? What if you created some randomizing code that makes each apple different, for eg custom mesh script that makes random shape apples.

I agree however that it would be nice to have methods for listing (and loading in runtime) all meshes in folder or all textures etc.

Make a Struct that has a boolean “bCanBeSpawned” and an ActorClass Variable (the purple one). The Class variable can also be of your MasterClass.
Go to the place in your code where you that Array.
Create a new Variable of the type of that Struct.
Convert this Variable into an Array.
Edit the variable and add all the Classes that your Admin should be able to be spawned.
The booleans should all be FALSE, as these Actors aren’t yet spawned and as far as I understood you, the Admin is only allowed to spawn them if the Actor was once in the level.

So now, when ever one of the Actors gets spawned into the Level you get that Array you created and loop over it.
You compare the Class of the spawned Actor with the entries and if you find an equal result you set the boolean to true.

Now, in some sort of widget, you get the Array too and loop over it. You add a button for each Class that the boolean is true for.
When ever you Update the Array, you should the also tell your widget to refresh with the new Array values.

UE4 has no “GetActorClassesOfActor” build in as you mostly don’t need this.
Always remember that only because you need that doesn’t mean it’s needed in the Engine.
If it’s so easy, then you are free to create a Pull Request on GitHub and share this function with us.

You could create a datatable based on struct that contains the classes of the items you want them to be able to spawn. (Could even include icons and other properties for items)

Create a UI that presents this table.

When they select item, spawn actor of that type of class.

I think your terminology is wrong. An Actor is, by definition, an object that has been spawned in the game.
Perhaps what you’re looking for is all the classes that derive Actor?

Also remember that a “dead zone” can be anywhere and any size. Anything spawned can come in any size defined, including .00001x and 10000x. Despite popular belief, size really doesn’t matter (at least in this case).

I am asking same questions, you worded it better.

I also had this problem while learning: I could not google for tutorials because i did not know keywords, when i knew keywords i did not need those turorials anymore.

So I hope that with some communication here we can find out what is really needed and how to do it.1

Thanks for the answers guys! I’ll give all of these a try until I find what I want and yea terminology was wrong sorry about that lol. I’ll let you guys know what I find.

Well using some of your guys’ ideas, I think I got it. I created a Structure with 3 fields. (Field 1 - String (ITEM ID), Field 2 - 2D Texture (ITEM ICON), Field 3 - ActorClass Reference (SPAWNABLE ACTOR)) Then in a new UMG Widget I created an event on Construct that casts to that structure and populates an array variable that I created (Item List(this is just a reference to the item structure I made)) then that populates a drop down menu with the items from the structure (Item Structure) and then I added a button. When you choose an option from the Drop down menu it breaks the structure (item structure) and gets the item ID string and then set the value of selection option to that value. Then when you click that button it runs a foreachloop of the item structure array I created and it breaks the structure down so I can see the values of the structure items and then compares the Item ID value with the selected option value of our dropdown menu and I run that through a branch node to see if they match and if so then I spawn actor at whatever vector coordinates. Which I have one made from a previous UMG so it spawns the items on the ground 250 centimeters in front of the admin. So it works pretty good and will work better with some tuning.

This is my Event OnConstruct Node
764b171d95f250f98ee338072d225fd6c79884af.png

This is my Event OnClicked Node for my Button
e9f65a6ff1eebf1db8aaab4c97c791cc24a95704.png

This is a list of my Variables (Item List is a Variable Reference to my Structure I made (ItemList Structure)
aef563a47213e357e6fc76693b4523efbaf97c3e.png

This window shows how I have to manually add each entry into my Commands_w Widget Item List Array.
185659a87f4b04ef2aea8e52365d74bad208848f.png

This is a old post but here is a solution using UObjectLibrary. Loading asset can take times.
UObjectLibrary* MyObjectLibrary = UObjectLibrary::CreateLibrary(AMyActor::StaticClass(), true, true);

MyObjectLibrary ->LoadBlueprintAssetDataFromPath(TEXT("/Game/PATH"));
MyObjectLibrary ->LoadAssetsFromAssetData();

TArray assetDataList;
MyObjectLibrary ->GetAssetDataList(assetDataList);

for (FAssetData& assetData : assetDataList)
{
if (UBlueprint* blueprintObject = Cast(assetData.GetAsset()))
{
if (UBlueprintGeneratedClass* blueprintGeneratedClass = Cast(blueprintObject->GeneratedClass.Get()))
{
const AMyActor* actorDC = Cast(blueprintGeneratedClass->ClassDefaultObject);
UClass* objClass = AMyActor->GetClass();
}
}
}