Replication - Spawning Actors with Material Dynamic Instance

Hello, I would like the clients to be able to spawn actors but with a visual effect that is made with dynamic material instance. This material has a scalar parameter that is adjusted at runtime. So when the actor is spawned a timer is set that is editing this parameter. I also want the actor to despawn after some time using the same visual effect. What is the correct way to replicate this?

Somehow I always end up with clients spawning actors but it’s not really working properly. When the first client spawns the actor, it works right and it’s also replicated - all clients see the actor and the effect. But when another client spawns the actor, it behaves as the actor was shared somehow - the actor spawns also for the clients that have previously spawned the actor and the visual effect plays for all actors. Also when there is one actor still spawned, if another clients spawns the actor it doesn’t play the effect - already spawns with the effect “completed”. And then both actors despawn at once with the effect.

For example: A player spawns an object on the ground and it spawns with something like a “fade-in” effect - a scalar paremeter of a dynamic material instance is adjusted right after spawn. The object then “fades out” after like 10 seconds and despawns. All players can spawn their own object but only 1 at a time.

This is quite easy to achieve but I can’t figure out how to replicate this. Thanks for the help.

It sounds like you are updating material parameters without instanciating a new dynamic material instance for each spawned object, so they’re all using a shared instance.

Assuming your material is on a mesh component, you can use function SetScalarParameterValueOnMaterials to update parameter values, this function will take care of automatically replacing mesh materials with new dynamic instances when needed.

Right now I am replicating the spawned actor itself using OnRep() function. In this function clients get the mesh and create a dynamic material instance. This doesn’t really work. Before I tried getting the mesh and creating material instance in multicast. That did the same.

I have another similar functionality already implemented and replicated. I am able to replace player’s character materials with a dynamic instance, play the effect and then after some time switch the materials back. Doing all of this via multicast and it works. But for some reason I can’t get this to work with spawning new actors. And I assume it is about how do I get the reference to the spawned actor’s mesh. It is the mesh that is shared, not the material instance. I mean, when I get character’s mesh instead of the spawned actor’s mesh, everything works right. All clients affect only their character mesh. But when I get the spawned actor’s mesh, it acts like I am globally affecting the mesh that I am spawning, because I can see that the mesh switches materials also in the content browser during runtime, while this doesn’t happen to the character mesh in the other case.

1 Like