Spawn Actor with "Expose on Spawn" properties in C++

I’m trying to change the color of a projectile that I’m spawning in C++ code. I have a projectile color property on the projectile class and I’m applying this color to a dynamic material instance which I’m creating inside the constructor. Is it possible to set a value to this property at the time the projectile is spawned in C++?

MyProjectile.h

// Projectile color
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Projectile")
FLinearColor* ProjectileColor;

This is on the character class:

FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.Instigator = this;
AMyProjectile* const Projectile = GetWorld()->SpawnActor<AMyProjectile>(MyProjectileClass, SpawnLocation, SpawnRotation, SpawnParams);

Perhaps there’s a better way of doing this, but I’m not sure how to set the value of the projectile color property so that the MyProjectile constructor can apply it to the dynamic material instance.

Thanks.

1 Like

Yes you need to use “DeferredSpawn” instead. That way you can initialize variables that you need to and then finish creating.

AMyActor* pActor = GetWorld()->SpawnActorDeferred<AMyActor>(MyActorTemplate, GetActorLocation(), SpawnRotation);
		if (pActor )
		{
                    pActor->//...setstuff here..then finish spawn..
			UGameplayStatics::FinishSpawningActor(pActor , FTransform(SpawnRotation, GetActorLocation()));
		}

Hope that helps =)

5 Likes

This is exactly what I need. Thanks Devero :slight_smile: