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"

How do I spawn ThirdPersonCharacter with C++

 FActorSpawnParameters params;
 params.bAllowDuringConstructionScript = true;
 params.bDeferConstruction = false;
 params.bNoFail = true;
 params.Name = {};
 params.ObjectFlags = EObjectFlags::RF_NoFlags;
 params.OverrideLevel = nullptr;
 params.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
 params.Template = nullptr;

 TSubclassOf< class AStaticMeshActor > character = nullptr;
 static ConstructorHelpers::FObjectFinder< UClass > found_something_object(TEXT("/Game/ThirdPersonBP/Blueprints/ThirdPersonCharacter.ThirdPersonCharacter_C"));
 character = found_something_object.Object;
 const FVector Location = { 0, 20, 0 };
 const FRotator Rotation = FRotator(0, 0, 0);
 auto something =
         GetWorld()->SpawnActor<AStaticMeshActor>    
     (character
         , Location
         , Rotation
         , params
         );


This code does not work. What is wrong?

Product Version: UE 4.18
Tags:
more ▼

asked Jun 23 '18 at 02:47 AM in C++ Programming

avatar image

pj_goldeneyes
3 1 1 2

avatar image ImmortalEmperor Jun 23 '18 at 04:45 AM

Is there an error output or just does nothing in engine? Also, for reference, where are you calling this?

avatar image pj_goldeneyes Jun 23 '18 at 06:32 AM

I called this from "void AMyActor::BeginPlay()". MYActor is located on the viewport in the UE editor.

But this crash on line 12. there is no erroe message. even could not to debug mode.

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

1 answer: sort voted first

First issue is that you can only use ConstructorHelpers::FObjectFinder in a classes constructor see here:

https://answers.unrealengine.com/questions/246199/how-to-use-constructorhelpersfobjectfinder.html

Have a look through the links on that answer and if you need any more help let me know.

more ▼

answered Jun 23 '18 at 06:41 AM

avatar image

ImmortalEmperor
115 4 5 7

avatar image pj_goldeneyes Jun 23 '18 at 11:59 PM

I want to use dynamic BluePrint loading and spawn it. This is a new code. But, It returned null at "character = bp->GeneratedClass;". What is wrong?

 auto *clazz = StaticLoadObject(UObject::StaticClass(), nullptr, TEXT("/Game/ThirdPersonBP/Blueprints/ThirdPersonCharacter"));
 if (clazz != NULL) {
     GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Green, TEXT("class exists!!!"));
     UBlueprint * bp = Cast<UBlueprint>(clazz);
     
     if (bp) {
         GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Green, TEXT("bp exists!!!"));
         auto character = bp->GeneratedClass;
         if (character != NULL) {
             FActorSpawnParameters params;
             params.bAllowDuringConstructionScript = true;
             params.bDeferConstruction = false;
             params.Name = { "test char" };
             params.ObjectFlags = EObjectFlags::RF_NoFlags;
             params.OverrideLevel = nullptr;
             params.Owner = this;
             params.Template = nullptr;

             const FVector Location = { 0, 20, 0 };
             const FRotator Rotation = FRotator(0, 0, 0);

             auto something =
                 GetWorld()->SpawnActor<AStaticMeshActor>
                 (character
                     , Location
                     , Rotation
                     , params
                     );
             if (something) {
                 GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Green, TEXT("spawn succeed!!!"));
             }
             else {
                 GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Red, TEXT("spawn failed!!!"));
             }
         }
         else {
             GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Red, TEXT("characer is NULL!!!"));
         }
     }
     else {
         GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Green, TEXT("bp does not exist!!!"));
     }
 }
 else {
     GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Red, TEXT("cls2 is NULL!!!"));
 }
avatar image pj_goldeneyes Jun 24 '18 at 03:43 AM

It works. Succeeded by using "LoadClass" instead of "StaticLoadObject".

(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