Hello! I’m a beginner in Unreal Engine and want to spawn an Actor declared in a blueprint. I’m using UE 4.13 and VS 2015. This is my current code of a C++ Actor which should spawn a blueprint actor on BeginPlay():
The code does absolutely nothing except printing “BeginPlay called” and if i uncomment the lines:
//FString a = NewActor->GetActorLocation().ToString();
//GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, a);
build the C++ source and play the game in the Editor, UE crashes. I suppose I’m doing something wrong with loading and/or spawning the blueprint. I also have the same problem with the first commented lines (with the UStaticMesh). Can somebody help me?
Most probably the NewActor reference is null, can you do a “if” branch after spawning the actor, to print a string if the NewActor is indeed null?
If that’s the case, then also add a if to check if the UClass *ClassToSpawn is null also, that could be your problem.
If the UClass is null, then the find object returned nothing, try to right click your blueprint (the one you want to spawn) and “Get Reference” or something like that, that will give you the full path of the blueprint,
“/Game/CubicColony/Blueprints/Zabawa/ZabawnaKostka.ZabawnaKostka” is not a Package. It is a class. “/Game/CubicColony/Blueprints/Zabawa/ZabawnaKostka” Is a package. Your code looks for a class it can cast to a package. Which will always return null. And Do Nothing.
Just use TSubclassOf<AActor> (or whatever class you want), way easier, less hard coded, and more user friendly.
// Pseudo code...
// Weapon.h
// In the editor, you'll be able to select an actor class from a drop-down menu, including BP classes.
UPROPERTY(EditDefaultsOnly, Category = "Parameters")
TSubclassOf<AActor> ProjectileClass;
// Weapon.cpp
GetWorld().SpawnActor<AActor>(ProjectileClass, GetActorLocation(), GetActorRotation());
How could one spawn it using BeginDeferredActorSpawnFromClass? I would like to initialise the spawned actor with some parameters before spawning but cant quite figure out how to do so.
I meet the same problem. I magrate my old project to new engine version UE 5.1, and still use C++ spawn blueprint actor. It works all in “Standalone Game” Mode, but after packaged, c++ can’t spawn blueprint by path.
I guess the may be the new engine version UE5.1 only package assets in world, but that blueprint actor only in content folder.
So I change the default project settings. Project Settings>Project>Packaging>Advanced>Cook everything in the project content directory.