x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Is it possible to assemble a list of classes of a type at runtime? (BP or C++)

I am working on a system to spawn random actors and thus need a random class. I would like to avoid making a array/map/etc of every class as they are created because it's fairly easy to make mistakes with (e.g., forgetting to add one.) I had the idea to use a program to generate a table by scanning all the files, but that would rely on being able to get a class by name which I couldn't find anything about being able to do it.

Edit for some clarity of what I am trying to accomplish:

Something similar to this, but instead of map names it provides class types.

I am creating an endless side scrolling shooter that spawns a random tile as you move forward. I plan on having many different types of tiles and as such I will have many blueprints subclassing e.g. TileBase. As far as I can find the only way to get a random class to spawn is by creating an array of the type of tile classes - thus every time I make a new tile I need to manually add a new entry into the array (this is, for example, how it's accomplished in the Infinite Runner tutorial.) There are ways to optimize this, of course, but that's beside the point.

My current solution is to use a data table. It has the advantage of being able to pair the class with a gameplay tag container to more easily filter the tiles to randomly choose from. As stated before, however, it opens up the issue of forgetting to add new entries to the table.

Product Version: UE 4.21
Tags:
more ▼

asked Jan 28 '19 at 01:53 AM in Blueprint Scripting

avatar image

Rakosman
58 3 9 12

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

Each class has corresponding UClass object in memory, so all you need to do is iterate theu them using TObjectIterator, filter them and add matching once to array or whatever you want

 TArray<UClass*> Classes;
 
 for (TObjectIterator<UClass> It; It; ++It)
 {
       if(It->IsChildOf(ABaseClassThatWeLookingFor::StaticClass())) {
              Classes.Add(*it);
      }
 
 }

Problem is it is expensive process even heavier then actor iteration with Get All Actors of Class, so you should do this once, in beginplay for example

In blueprint it impossible... i think? if you can iterate objects you should be able to. Generly it would be better if you do this in C++ for performance and you can also re use array created in C++ in blueprint.

more ▼

answered Jan 28 '19 at 05:21 AM

avatar image

Shadowriver
37.3k 936 172 1117

avatar image mightyenigma Jan 30 '19 at 12:36 AM

I agree - do it once and store the results before anything requiring responsive gameplay happens during runtime.

avatar image Rakosman Feb 01 '19 at 05:14 PM

Is this generating a list of all classes, or a list of classes that are in use?

And yeah, this is definitely something that would be cached. I'm just trying to avoid needing to manually add a new entry to a static array every time I make a new child.

avatar image Shadowriver Feb 02 '19 at 06:40 PM

It will list all UClass in memory, this means all UClass of all loaded modules and Enigne module alone which is loaded all the time in all games, Sadly this is only way to list classes that are in memory as C++ is low level language and once compiled any class information is lost, and engine don't track classes used (atleast i didnt find any case of that, engine is so huge you always find something new about it everyday). and it frequently used methods also in engine code, if you search github by TObjectIterator<UClass> you will find countless examples of use, you might also find methods use to optimize it in them.

But since you want to list classes "in use" there is method to do so, by making objects that you spawn register the class in the array. This method is used to register created perticilar objects so there no need to search for them in memory which is heavy process. But this can be used to register used classes too. You can only do this when object is placed in editor (actor code is active then too) or on BeginPlay, so objects actully have a chance to do so.

Just make SomeGameModePointer->SomeClassArray.AddUnique(GetClass()); and it will gather all classes that been spawned, you would also need extra code to delete class from array, that might would require some actor counting and invidual object listing

avatar image Rakosman Feb 06 '19 at 03:59 AM

You misunderstand me. Right now when I create a new child class blueprint I have to manually add it's class type to a data table, then when I spawn a class I pick random entry from that table.

As stated in my original post, I'm not looking for a list of classes in memory, I'm looking for a list of all the possible child classes. Since I'm trying to avoid needing to manually make the array, the array would have to be made at run time.

avatar image mightyenigma Feb 06 '19 at 07:12 AM

I wonder if the AssetManager class could help

avatar image Rakosman Feb 06 '19 at 09:30 PM

I looked into it but it's not really getting me anywhere. Seems like that is mostly for getting assets available, so I still wouldn't have a list of class objects from which to spawn classes.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question