Pattern for Actor reference when multicasted?

Hi!

I have a function (spawnProjectileServer) which runs on the server that spawns a projectile. From that I can save a replicated variable (currentProjectile) as a reference to that projectile. On a given key press, I can call a function on that projectile to destroy itself at any given time.

The issue here, is the projectiles movement is awful. It’s very janky.

So we use the spawnProjectileServer function to call spawnProjectileMulticast which multicasts the projectile on each client. The moment is very smooth. It looks great.

However, now I don’t have access to that reference I was using to destroy the projectile.

I’ve kind of exhausted my options. Any guidance or suggestions would be a great help.

Thanks

Depending on the complexity of the projectile’s path, you can get away with using a projectile movement component, and have the replicated projectile run the simulation on all the clients as it would on the server

That means don’t replicate its movement, because as far as I know, Projectile Movement Component has replicated movement by default

That way, when a projectile gets replicated, it will spawn at the original location, and then start simulating the same movement on all clients

You can also drive some of the movement properties (such as speed, gravity) through replicated properties - simply create Initial Only Replicated variables for the properties you want to change, disable Auto Activate on the projectile movement, and then on begin play set the properties using the variables that you created on the movement component, and then activate it to begin the simulation

If you’re familiar with C++, you can create your own projectile class, and override the replicated movement functions virtual void PostNetReceiveVelocity(const FVector& NewVelocity) and virtual void PostNetReceiveLocationAndRotation() to set the relevant values on the projectile movement component. I think I’ve done this before and it did make replicated projectiles smooth

Projectile movement component also has the option to interpolate a component, which might help make the projectile look smooth if you go with just normal replicated movement, since you can set the VFX for example to interpolate to the location of the projectile instead of following it attached, which should smooth out the movement. I haven’t tried this approach however, so I’m not sure about how good of a result you can achieve

Another option is client-fakey

You can add an int ID to your projectile and pass in the ID when destroying.

Multicast_SpawnProjectile(int projectileID)
{
//... spawn projectile 
//... projectile.mID = porjectileID;
};

Multicast_DestroyProjectile(int projectileID)
{
//... porjectileArray = GetActorOfClass() or if stored in an array
//... loop porjectileArray and destroy the found ID
};