Sleeping Bag DestroySolved

Hello everyone, I can not track the destruction of the player's sleeping bag -
Used the following methods:

        void OnEntityDeath(BaseCombatEntity entity, HitInfo info, BasePlayer player)
        {
            if (entity == null || player == null) return;
            if (entity.ShortPrefabName != "sleepingbag_leather_deployed") return;
            if (!DataBasePlayer.InfoData.ContainsKey(player.userID)) return;
            DataBasePlayer.InfoData.Remove(player.userID);
            SendReply(player,"Спальник сломан - хоум удален");
            SaveData();
        }
        void OnEntityKill(BaseNetworkable entity, BasePlayer player)
        {
            if (entity == null || player == null) return;
            if (entity.ShortPrefabName != "sleepingbag_leather_deployed") return;
            if (!DataBasePlayer.InfoData.ContainsKey(player.userID)) return;
            DataBasePlayer.InfoData.Remove(player.userID);
            SendReply(player,"Спальник сломан - хоум удален");
            SaveData();
        }

        void CanPickupEntity(BasePlayer player, BaseCombatEntity entity)
        {
            if (entity == null || player == null) return;
            if (entity.ShortPrefabName != "sleepingbag_leather_deployed") return;
            if (!DataBasePlayer.InfoData.ContainsKey(player.userID)) return;
            DataBasePlayer.InfoData.Remove(player.userID);
            SendReply(player,"Спальник сломан - хоум удален");
            SaveData();
        }

None of the methods above work.

Have you tried...

void OnEntityDeath(SleepingBag sleepingBag, HitInfo info, BasePlayer player)
​

 

It's important to ensure that you're using the right hooks and passing the correct parameters. From your code, it seems that you may be using parameters that don't exist where the hook fires in the game. I recommend taking a look at the following documentation for more information on the available hooks:


What you're looking for is the OnEntityDeath hook.
        private void OnEntityDeath(BaseCombatEntity entity, HitInfo hitInfo)
        {
            // Obtain the sleeping bag and don't proceed if it's invalid or doesn't exist.
            SleepingBag sleepingBag = entity as SleepingBag;
            if (!sleepingBag.IsValid())
                return;

            // Obtain the attacking player.
            BasePlayer attacker = hitInfo.Initiator as BasePlayer;
            if (!attacker.IsValid())
                return;

            // Obtain the sleeping bag owner's id.
            ulong sleepingBagOwner = sleepingBag.OwnerID;
        }

 

From there, you can cast the entity as  SleepingBag to get the specific sleeping bag that was destroyed.

To obtain the attacking player, you can use hitInfo.Initiator and cast it as a BasePlayer, or use hitInfo.InitiatorPlayer directly. And to get the owner of the sleeping bag, use sleepingBag.OwnerID, and then look up the player's ID if you need to obtain the BasePlayer from it.

Additionally, you may want to consider adding a check to ensure that the attacking player is not an NPC. You can do this by checking their steam ID using attacker.userID.IsSteamId()

You can also cast the type in the hook signature, which saves you from having to check the type in the method.

private void OnEntityDeath(SleepingBag entity, HitInfo hitInfo)​

Thank you my friends!

Locked automatically