[Question] Destroying replicated actor?

Hi everyone, I’m working on a multiplayer shooter project, but I’m having some trouble getting my gun system to work. Here’s how it’s laid out:

  • When the gun is equipped, spawn a gun actor and attach it to the owner’s hand socket (this is working)
  • When the gun is not equipped, destroy the gun actor completely (not working)

I think the issue is with my replication of the gun actor, but I have no idea how to do this right.

Here’s the screenshot of how I spawn the gun actors:

https://gyazo.com/e5885ad7653ba1f7d992554b14b0ad90

And how I cast to and destroy the gun actor:

https://gyazo.com/6ba9066d90cdaa504fe118ad9105c998

(in the gun actor BP)

https://gyazo.com/6f49ace8fb22beda87bbae1c4bddac10

If anyone has experience with destroying actors over multiplayer i’d really appreciate any help, as i’ve been tearing my hair out over this for a good week. Thanks a bunch!

Hi,

Have you tried destroying it only on the Server?

Sorry for the late reply; I assume you meant the “Destroy” event in the gun bp? If so, then it still doesn’t work on the client but it still destroys the gun for the server.

First, I would suggest you to make your RPC functions in the following fashion:



EquipPistol()
{
   //Equip pistol logic.
}

Server_EquipPistol()
{
    EquipPistol();
}


The EquipPistol() function is not replicated but, the Server_EquipPistol() is, so you can use the same EquipPistol() function both on client and server side, without having to duplicate your code.

Regarding your actual issue, it might be a stupid question but do you have your Actor actually set to replicate?
In my last post I was suggesting that you probably don’t have to use a replicated function to destroy the Actor. If the Actor replicates, and it gets destroyed on the server, the clients should also auto-destroy them, without having to explicitly tell them. I might be wrong, but I would definitely try this.

I changed what you suggested and it’s still not working on the client. The gun actor has been replicating. Your logic should work and I’ve set up this system that way in one of my many attempts to solve this, but I believe at heart this issue is due to the replication of my gun reference variable set after spawning it. After the client tries to unequip the gun, I get an error saying it’s pending kill from this ( https://gyazo.com/6ba9066d90cdaa504fe118ad9105c998) macro

Which class is that on the screenshot?

I am very confused looking at your graph. You do not need to spawn the pistol twice, only once, on the server. If the pistol is set to be replicated, it will be visible for the clients as well. Use a SwichHasAuthority node before you spawn your pistol and only spawn it on the Authority pin.

Double check if your pistol replicates, and only destroy it in a non-replicated OnAuthority guarded CustomEvent called for example DestroyPistol(). If you have an unequip animation, trigger a Multicast CustomEvent from the DestroyPistol() CustomEvent playing the animation etc, and set a Delay before actually destroying the pistol (in the DestroyPistol(), not in the Multicast event).

I hope this will help you somewhat.

Is this what you mean? https://gyazo.com/4755660cf85f1e588acfe6adb2b570bd

That is in the character BP and the screenshot in my previous reply was in a macro that is called when the pistol is equipped.

I’m kind of confused what you mean by OnAuthority guarded event, I assume that you use C++

Sorry, I meant the OnSwitchHasAuthority node.

Yes, this is what I suggested, is this not working? Are you a 100% sure that your Pistol class replicates (not only the reference but the class itself)?

This only works for the server, when the client plays I get this error:

https://gyazo.com/84d49b5db8e8c25a8bd807d7f2cf11c1

Also these are the class defaults for the pistol, I’ve tried it with always relevant checked and not checked:

https://gyazo.com/f4dbeda854b95b9f27a4fe62d0715352

It looks like you are trying to access the PistolRef after the DestroyActor was called. PendingKill error usually means that the object has been garbage collected, but not completely removed from the memory yet (but unsafe to use).

It is generally a good practice the check if a reference is valid, before you are trying to use it. You can do this with the IsValid node. Only use references that return Valid and you should prevent the error from happening.

Right, I understand that much but adding isValid before would just mean that the actor doesn’t even try to be destroyed. This is why I think it’s an issue with the referenced gun being replicated as a variable :confused:

No, you get the PendingKill error, because you are trying to access the reference AFTER you have called destroy on it.
Locate each place where the PistolRef is used, and ensure that nothing is trying to access it after you have called the DestroyActor function.

EDIT: If you are not using it elsewhere, turn off the replication on the reference variable, as you do not need it to be replicated then!

The only getter for it is with the destroy actor event. Either way it shouldn’t interfere with the destruction of the actor right?

Are you calling the DestroyPistol() from the server? You should only call it after the Authority node!

I’ve tried it both from the server and not, both times after an authority node.

Okay, I have tested it an it is working for me, I have made screenshot for you:
4ec9d36ba0b2938768e7f1276ede0be3d1ffc864.jpeg

yES! PERFECT!@ THANK YOU SO MUCH!! Really this helped a lot, thank you sooo much i love you <3 <3

I’m glad that I could help, good luck with your game! :slight_smile: