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"

Spawn derived class at runtime (blueprint derived from C++)

Let's say I have a property in my actor class:

 UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Magic)
 TSubclassOf<ASpellProjectile> testProjectileClass;

I want to set this property in editor (by dragging Blueprint derived from ASpellProjectile into property), and then spawn the class at runtime.

Problems:

First, for some reason this doesn't work:

 ASpellProjectile *tmp = Cast<ASpellProjectile>(testProjectileClass);

in this example tmp will always be null.

Second, attempting to spawn actor being referenced:

 ASpellProjectile *projectile = 
     world->SpawnActor<ASpellProjectile>(testProjectileClass->GetClass(), transform, spawnParams);

Results in message:

  SpawnActor failed because BlueprintGeneratedClass is not an actor class

So, how do I actually spawn an actor from "template"/"preset"?

Product Version: UE 4.10
Tags:
more ▼

asked Nov 30 '15 at 02:46 PM in C++ Programming

avatar image

NegInfinity
101 13 17 28

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

Hello NegInfinity,

You should be able to fix this by making one change to the lines of code you provided. In this line:

 ASpellProjectile *projectile = world->SpawnActor<ASpellProjectile>(testProjectileClass->GetClass(), transform, spawnParams);

Remove the "->GetClass()" part and just leave the variable name "testProjectileClass". This should let the function know that you intend to spawn that class. You can find more information about this kind of setup from the following video.

https://wiki.unrealengine.com/Videos/Player?series=PLZlv_N0_O1gYup-gvJtMsgJqnEB_dGiM4&video=qZHMf2WqjSM

Hope this helps!

more ▼

answered Nov 30 '15 at 07:38 PM

avatar image

Matthew J ♦♦ STAFF
31.8k 526 91 417

avatar image NegInfinity Dec 01 '15 at 11:45 AM

Thanks for the response. I was having trouble because I thought that testProjectileClass points at object instance to clone (like unity prefabs), not at object class. Someone mentiond on the forums that it points at class, not instance, and that cleared things up.

(comments are locked)
10|2000 characters needed characters left
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