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"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

Spawn an Actor from a TArray of TsubClassOfs

I have a TMap of some integer keys and then a TSubclassOf. The TMap will be randomly populated with about 6 subclasses of 'TaskBase' meaning they all are different classes with the same parent. After being populated, they will then be selected in random order and then their individual functions will need to be accessed.

In order to do so, I must be able to spawn an instance of the TSubclassOf. How do I get the type of the specific TSubclassOf and how do I utilize it in the basic GetWorld()->SpawnActor(...) function?

Thanks.

Product Version: UE 4.22
Tags:
more ▼

asked May 18 '19 at 03:38 AM in C++ Programming

avatar image

Chibey
2 1 3 5

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

1 answer: sort voted first

Hey.

I wrote the following code to automatically spawn appropriately high climbable/ vaultable walls and lights from a JSON file.

alt text

You should have similar implementation for a TMap. SoftClassPtr and SubClassOf are similar.

Here is the implementation. Do read the notes.

alt text

Hope this helps.

Edit 1: I should add just in case,

  • you might want to opt for a static function which works for multiple classes.

  • The following templated function does that.

  • It is harder to implement in your codebase.

  • but in case anyone looks for it, here it is.

alt text

If you want even more control, instead of mentioning class in template, you can just get class with one line of code. But it might be a bad idea because if that class is not included in your calling class, you will have a hard time catching the errors.

Hope this helps moar.

zzdtstruct.png (13.5 kB)
screenshot-305.png (65.5 kB)
more ▼

answered May 18 '19 at 06:23 AM

avatar image

DebashishPatra
24 1 1

avatar image Chibey May 18 '19 at 01:07 PM

Just implemented something similar and it compiled! I'll test it when I get home later in the evening and report back. Quick question though. I did try and use the templates previously but was having a ridiculous amount of trouble setting them up with UE4. Is there any good documentation you could recommend? Or is that last snippet you uploaded just a raw CPP class? Thanks again for all the help.

avatar image DebashishPatra Jun 01 '19 at 01:34 PM

These methods are not documented anywhere. You have to understand the core API and build your way from there. For the same reason, most of such features are not available to blueprint users. The last snippet is a pure C++ header file that statically links to most used functions.

(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