Jump to content

How to get the ObjectReference for a dead NPC's dropped weapon?


Recommended Posts

The weapon does not have a RefId. It is not an ObjectReference. It is part of the creature model, just as it was when the creature was still holding it before it died. At best, you can get a BaseId for it. You could try doing a GetEquippedWeapon() on the corpse but that might not work - I don't know, I haven't tried. Otherwise, as Reneer said, you will have to do it before the creature dies and record the information somewhere.

Edited by OldMansBeard
Link to comment
Share on other sites

@OldMansBeard

 

Thank you for posting your thoughts ...

 

Yes, the get EquippedWeapon on the corpse does return None. None is also returned when directly requesting an owner reference from the weapon. Same for faction owner. [ I am using a dead Raider here for discussion purposes ]

 

I can get a BaseID of the owner from the weapon, but it seems nothing significant can be done with that -- very limited set of functions available. I had hoped to use GetClass on a weapon's owner BaseID as a way to at least match that class with a (hopefully) appropriate nearby Corpse's class, but the class value changes when an Actor dies; e.g. it is no longer "raider". The idea was to maybe only process corpses for the very few defined "bad guy" classes (and leave the "good guys" to fend for themselves). Maybe I could do that same thing more broadly using the BaseID's themselves, but there are an awful lot of them.

 

The issue that I am trying to avoid is this: if the bot's scan overlaps a shopkeeper's inventory where weapons are out on display, or overlaps a weapons rack a player has placed in a home, those weapons will all be taken if they are not filtered out. To avoid that, being able to directly associate a specific weapon with a specific corpse would provide that filtering mechanism.

 

Actively tracking all of the NPCs within a settlement to know the weapon that was readied/used when they die ... seems likely to be successful, but it also seems potentially beyond the scope of this mod. I'll give it further thought; the suggestion is appreciated.

Link to comment
Share on other sites

@akiras404

 

If a corpse is disabled, the game automatically disables the dropped weapon ... but the game also immediately deletes that weapon right then as well, even though the corpse has not yet been deleted. And if a disabled corpse is re-enabled, the weapon remains deleted. There is no way to un-delete it -- once Delete() is invoked on something, there is no way to restore it back to how it was. I discovered all of this last week while experimenting, and so set that approach aside as being unsuccessful.

 

But ... your post caused me to begin wondering: what would actually happen if my bot tried to take an already deleted weapon? Doing that didn't really make any sense. But I tried it anyway.

 

The answer is that the deleted weapon is not affected. It remains deleted, and like all deleted things is eventually purged from the game.

 

However ... an exact *copy* of that weapon is placed in the bot's inventory!!! A perfectly usable item that is itself not flagged as deleted, does not disappear when the corpse is gone, and which can be used normally thereafter. Perfect.

 

So your suggestion was excellent! It sparked me to try something that I was quite sure would fail. But it worked!

 

Thank you, and a Kudo, for helping me solve this problem.

 

Athelbras

Link to comment
Share on other sites

Final Solution:

 

The idea was to have scripting that removes dead things from the game, but which also recovers items from that corpse, including its dropped weapon.

 

The RemoveAllItems() function works correctly to take everything from the Corpse. But not the dropped weapon, because it is not actually in the corpse's inventory.

 

I am not able to directly determine the weapon an Actor dropped when it died: when using scripted functions on the corpse there is no way to get an object reference for the weapon, and when using scripted functions on the weapon there is no way to get an object reference for the corpse.

 

So what I do is use the FindAllReferencesWithKeyword function, with ObjectTypeWeapon as the keyword, to acquire references to all weapons in the area. Found weapons that are currently disabled or deleted are removed from that list of known weapons.

 

Then, for a specific corpse, I use Disable() on that corpse which causes the game to disable both the corpse and its dropped weapon and also causes the game to automatically delete the weapon. Next, I examine each of the weapons acquired via that "Find" function to see if one of them is now deleted. If there is a deleted weapon, then that is the dropped weapon. If there is no deleted weapon, then none was actually dropped.

 

As a final step, the AddItem() function is used to acquire the deleted weapon. This function does not actually take the weapon (because it is deleted), but instead acquires a usable copy of the weapon.

Link to comment
Share on other sites

Final Solution:

(...)

 

As a final step, the AddItem() function is used to acquire the deleted weapon. This function does not actually take the weapon (because it is deleted), but instead acquires a usable copy of the weapon.

 

Does this keep legendary items or heavily modded guns intact? Meaning, do you add the RefID (which then spawns as a copy of that RefID, with a new ID) or the BaseID?

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...