Ways to change Projectile Movement Component parameters during its spawn?

So I’m at the part in Toon Tanks where we get to spawn the projectile and move it, which is great.

However, I do have a question on it.
So hypothetically, if I were to create 10 different kinds tanks and each tank will have a different kind of initialspeed and maxspeed for its projectile. Are there ways to change the projectilemovement parameters at its spawn? Or will I have to end up creating 10 different kinds of projectile blueprints, 1 for each tank?

As a reminder for those who don’t remember this lesson anymore, we have a BasePawn class that has a ‘fire()’ function, which has a spawnactor function (for spawning the BluePrint version of the Projectile class).

void ABasePawn::Fire()
{
	auto Projectile = GetWorld()->SpawnActor<AProjectile>(ProjectileClass, ProjectileSpawnPoint->GetComponentLocation(), ProjectileSpawnPoint->GetComponentRotation());
}

And in the Projectile’s Constructor, that’s where all the UProjectileMovementComponent thing happens.

AProjectile::AProjectile()
{
	PrimaryActorTick.bCanEverTick = false;
	
	ProjectileMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Projectile Mesh"));
	RootComponent = ProjectileMesh;
	
	InitialSpeed = 2500.f;
	MaxSpeed = 4000.f;

	ProjectileMovementComponent = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("Projectile Movement Component"));
	ProjectileMovementComponent->bRotationFollowsVelocity = true;
	ProjectileMovementComponent->InitialSpeed = this->InitialSpeed;
	ProjectileMovementComponent->MaxSpeed = this->MaxSpeed;

}

First I had the idea of Constructor Overloading, but I have no clue how to do that in combination with the SpawnActor().

And the second thing I tried was grabbing the pointer to the spawned projectile and somehow change the InitialSpeed and MaxSpeed of the UProjectileMovementComponent. But I couldn’t make it work lol.

I could always just simply make a million different BluePrints versions of the projectile, but I would find it quit inefficient that way.

You can use SpawnActorDeferred which doesn’t call BeginPlay until you call FinishSpawningActor. See here for how/when things get called

Actor Lifecycle | Unreal Engine Documentation

const FVector Location = ProjectileSpawnPoint->GetComponentLocation();
const FRotator Rotation = ProjectileSpawnPoint->GetComponentRotation();

AProjectile* Projectile = GetWorld()->SpawnActorDeferred<AProjectile>(ProjectileClass, FTransform(Rotation,Location));

// Do stuff

UGameplayStatics::FinishSpawningActor(Projectile, FTransform(Rotation, Location));
1 Like

Thanks! This solves exactly the issue I was having!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.