ViaCognita
(ViaCognita)
1
Hi!
I’m using Unreal 5.1.1.
I want to spawn Blueprint actors in C++ with this code.
Header file:
UPROPERTY(EditDefaultsOnly, Category = "Planetarium")
class AStarActor* BlueprintStar;
Code file:
void AStarsSphereActor::ShowStars(TArray<FStar> Stars)
{
if ((BlueprintStar) && (Stars.Num() > 0))
{
const FAttachmentTransformRules AttachmentTransformRules =
FAttachmentTransformRules(EAttachmentRule::KeepWorld, true);
for (FStar& star : Stars)
{
FVector Location;
Location.X = star.SphereLocation.X;
Location.Y = star.SphereLocation.Y;
Location.Z = star.SphereLocation.Z;
AActor* StarActor =
GetWorld()->SpawnActor(
UGameplayStatics::GetObjectClass(BlueprintStar), &Location);
StarActor->AttachToComponent(RootComponent, AttachmentTransformRules);
}
}
}
But I need to have a BP_Star object inside the level to use it.
I only wanted to know how to set the Blueprint class of the BP_Star in the Blueprint class that inherit from AStarsSphereActor.
How can I do it?
Thanks
MB.P
(MB.P)
2
Maybe you should write like this:
UPROPERTY(EditInstanceOnly, Category = Components)
TSubclassOf<class AStarActor> BlueprintStar;|
and
if (*BlueprintStar)
{
***
SpawnActor(BlueprintStar->Get(), &Location);
***
}
ViaCognita
(ViaCognita)
3
Thanks, but there is a for:
for (FStar& star : Stars)
I want to set the Blueprint class in the editor to know which kind of class I have to spawn N times.
Thanks.
MB.P
(MB.P)
4
header:
UPROPERTY(EditInstanceOnly, Category = Components)
TSubclassOf<class AStarActor> BlueprintStar;
cpp:
void AStarsSphereActor::ShowStars(TArray<FStar> Stars)
{
if (*BlueprintStar)
{
const FAttachmentTransformRules AttachmentTransformRules =
FAttachmentTransformRules(EAttachmentRule::KeepWorld, true);
for (FStar& star : Stars)
{
FVector Location;
Location.X = star.SphereLocation.X;
Location.Y = star.SphereLocation.Y;
Location.Z = star.SphereLocation.Z;
AActor* StarActor =
GetWorld()->SpawnActor(
BlueprintStar->Get(), &Location);
StarActor->AttachToComponent(RootComponent, AttachmentTransformRules);
}
}
}
You can set the property “BlueprintStar” in the editor to specify a BP class that inherit from AStarActor.
By the way, you can also replace the class AStarActor with other classes.