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 to spawn actor from BP?

Can you advice me how to spawn actor from BP?

Today I have the whole day trying to solve this problem and has not yet been able to solve it.

  auto cls = StaticLoadObject(UObject::StaticClass(), nullptr, TEXT("/Game/Cube_Blueprint"));
  UBlueprint * bp = Cast<UBlueprint>(cls);

How to spawn bp object?

Product Version: UE 4.8
Tags:
more ▼

asked Jun 23 '15 at 04:54 PM in C++ Programming

avatar image

Arthur Khusnutdinov
605 89 95 250

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

3 answers: sort voted first

I lost 2 days to solve this problem...

But I made solution!

 void AMyNativePlayerController::MyLoadClassByName10(const FString& Path)
 {
 
 
     auto cls = StaticLoadObject(UObject::StaticClass(), nullptr, TEXT("/Game/Wood_Table_Blueprint"));
     if (!cls)
         GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("Failed to load UClass " + Path));
     else
         GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("UClass LOADED!!!!"));
 
     UBlueprint * bp = Cast<UBlueprint>(cls);
 
     if (!bp)
         GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("Failed to load UClass 2  " + Path));
     else
         GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("UClass LOADED!!!! 2 " + bp->GetName()));
 
     TSubclassOf<class UObject> MyItemBlueprint;
 
     MyItemBlueprint = (UClass*)bp->GeneratedClass;
     UWorld* const World = GWorld->GetWorld();
     if (World){
 
         FActorSpawnParameters SpawnParams;
         //SpawnParams.Instigator = this;
         UObject* DroppedItem = World->SpawnActor<UObject>(MyItemBlueprint, { 0, 0, 0 }, { 0, 0, 0 }, SpawnParams);
 
     }
     else {
         GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("NO WORLD!!!!"));
     }
 
 }
more ▼

answered Jun 23 '15 at 05:14 PM

avatar image

Arthur Khusnutdinov
605 89 95 250

avatar image DerChris Aug 26 '15 at 08:21 PM

That worked perfectly for me, thanks a lot. Here's s copy&paste version for anyone:

 TSubclassOf<class UObject> Common::FindOrLoadBluePrintClass(const TCHAR* path)
 {
     // finds or loads a blueprint class. Needed to create BP based objects in C++.
     /*
         // usage:
         TSubclassOf<class UObject> BP_Item3DActor= FindOrLoadBluePrintClass(TEXT("/Game/FirstPerson/Blueprints/BP_Item3DActor"));
 
         AItem3DActor* t = Common::world->SpawnActor<AItem3DActor>(BP_Item3DActor, wPos, SpawnRotation, SpawnParams);
         // 't' actually points to a BP class derived from C++ class 'AItem3DActor'
     */
 
     UObject* something = StaticLoadObject(UObject::StaticClass(), nullptr, path);
     UBlueprint* bp = Cast<UBlueprint>(something);
     TSubclassOf<class UObject> MyItemBlueprint;
     MyItemBlueprint = (UClass*)bp->GeneratedClass;
 
     return MyItemBlueprint;
 }
 



 
avatar image MarkcusD Mar 21 '16 at 02:15 AM

Hi,

I tried this and it works in editor, but when I package the game it can't find the BP class. Any ideas?

avatar image Greywacke Sep 10 '17 at 08:41 PM

I'm having the same issue too. The object cannot be found in the packaged version, but works fine in standalone and editor.

avatar image darkgaze Aug 28 '18 at 10:51 AM

In case somebody is looking for this. If you need the "Reference" to an object, you can just find it in your UEditor browser, right click, COPY REFERENCE -> It will return something that you can paste in your code: example: Blueprint'/Game/Blueprints/BPPortal.BPPortal'

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

Do you want to spawn Object or Actor? If actor use:

World->SpawnActor(ProjectileClass, MuzzleLocation, MuzzleRotation, SpawnParams);

This code is from: https://wiki.unrealengine.com/First_Person_Shooter_C%2B%2B_Tutorial#Adding_Projectiles_and_Shooting

If object, use:

UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject);

UObject* tempObject = ConstructObject(UC);

EDIT: I just realized what do you want to achieve. Look at this code:

ConstructorHelpers::FObjectFinder BlueprintReference(TEXT("SomePath")); ConstructObject(BlueprintReference.Object->GeneratedClass);

more ▼

answered Jun 23 '15 at 05:09 PM

avatar image

Eruptor Design
19 1 2 7

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

I had the same issue and struggled a bit to get the solution from the first answer to work from blueprints - so for everyone who has the same problem. First of all i created a c++ blueprintfunction library, here the header:

 // Fill out your copyright notice in the Description page of Project Settings.
 
 #pragma once
 
 #include "Kismet/BlueprintFunctionLibrary.h"
 #include "MyBlueprintFunctionLibrary.generated.h"
 
 UCLASS()
 class PICKDROPPROTOTYP_API UMyBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
 {
     GENERATED_BODY()
     
 public:
    UFUNCTION(BlueprintCallable, BlueprintPure, Category="SunShine")
    static TSubclassOf<class UObject> FindOrLoadBluePrintClass(FString path);
 };

Here the cpp:

 // Fill out your copyright notice in the Description page of Project Settings.
 
 #include "PickDropPrototyp.h"
 #include "MyBlueprintFunctionLibrary.h"
 
 TSubclassOf<class UObject> UMyBlueprintFunctionLibrary::FindOrLoadBluePrintClass(FString path) //const TCHAR* path)
 {
    TSubclassOf<class UObject> MyItemBlueprint = NULL;
 
    UObject* something = StaticLoadObject(UObject::StaticClass(), nullptr, *path);
    if(something != NULL)
    {
       UBlueprint* bp = Cast<UBlueprint>(something);
       if(bp != NULL)
       {
          if(GEngine)
             GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, FString::Printf(TEXT("found some shit")));
          MyItemBlueprint = (UClass*)bp->GeneratedClass;
       }
       else
       {
          if(GEngine)
             GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, FString::Printf(TEXT("bp is NULL")));
       }
    }
    else
    {
       if(GEngine)
          GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, FString::Printf(TEXT("something is NULL")));
    }
 
    return MyItemBlueprint;
 }

And here a BP example how to use it (the path you can just get by the content browser, right-click on some StaticMeshActor and select "Copy Reference")

alt text The spawn location is just some static thing which i add to the camera, you can get the location from where ever you like to spawn a new object ;)

more ▼

answered Feb 03 '17 at 03:18 PM

avatar image

Leorekk
31 3 8

(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