Download

Dynamically Spawning replicated Actors with custom defaults

Hi All,
First post!
I’m diving into learning Unreal and C++ with a focus on getting to grips with multiplayer.

I’ve got this all working on hosted and dedicated server but I wanted to check I was approaching this correctly as I couldn’t find many references to check and I wasn’t quite sure if I was just getting lucky with timing on the conditional replication and begin play or if I needed to be more explicit like setting a ReplicatedUsing

I’ve got a weapon that fires projectiles but the projectiles vary in size/damage/speed etc.

In the weapon class

I’m using deferred spawn to let me change the Actors defaults before it spawns on the server like this, I’ve created a struct in the projectile actor for the dynamic values I want to change the values increase the longer the player hols the trigger down or they can fire lots of low damage small shots.



ElectricBolt = world->SpawnActorDeferred<ASProjectileBase>(ProjectileClass, Transform, MyOwner, MyOwner->GetInstigator(), ESpawnActorCollisionHandlingMethod::AlwaysSpawn);
if (ElectricBolt)
{
ElectricBolt->ProjectileDefaults.LaunchVelocity = BoltCurrentVelocity;
ElectricBolt->ProjectileDefaults.LaunchDirection = EyeRotation.Vector().GetSafeNormal();
ElectricBolt->ProjectileDefaults.BaseDamage = BoltCurrentDamage;
ElectricBolt->ProjectileDefaults.DamageSphereRadius = BoltCurrentHitBox;
ElectricBolt->ProjectileDefaults.ParticleSize = ParticleCurrentSize;
ElectricBolt->SetOwner(MyOwner);
UGameplayStatics::FinishSpawningActor(ElectricBolt, ElectricBolt->GetTransform());

}


Then in the projectile class (I am only ever spawning this via server from the owner actor then the actor is set to replicate so that handles that for me).

I have the struct set up to replicate with a COND_InitialOnly I think that just replicates once when it initializes the documentation is not clear to me but that’s what it seems to do, this seems to work i.e. the proxies of the projectile all look the same size etc. as the one fired on the server but this is the bit I’m not sure if I’m just getting lucky on timing or not i.e. could begin play fire before the defaults get replicated or are they guaranteed to get replicated the same time the actor does.



void ASProjectileBase::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);

DOREPLIFETIME(ASProjectileBase, bExploded);
DOREPLIFETIME_CONDITION(ASProjectileBase, ProjectileDefaults, COND_InitialOnly);
}


Then in Begin Play I set the default values




void ASProjectileBase::BeginPlay()
{
Super::BeginPlay();

SetProjectileDefaults();


}



void ASProjectileBase::SetProjectileDefaults()
{

SetLifeSpan(ProjectileDefaults.Lifetime);
SphereComponent->SetSphereRadius(ProjectileDefaults.DamageSphereRadius);
ProjectileMovementComponent->Velocity = ProjectileDefaults.LaunchVelocity * ProjectileDefaults.LaunchDirection;
ProjectileEffectBase->SetWorldScale3D(FVector(ProjectileDefaults.ParticleSize));

}



thanks!

Mark