Jump to content

Deleting unloaded NPCs


WIGWAM

Recommended Posts

I'm having trouble getting NPCs to delete once they have been unloaded.

 

The NPC is spawned by another script so I can not set the NPC as a property, so I currently have this script running on the actor base:

Scriptname Script extends Actor

GlobalVariable property MaxCritterCounter auto ;Counts upwards

Event OnUnload()
	self.Disable(true)
	self.Delete()
	MaxCritterCounter.SetValue(MaxCritterCounter.GetValue() - 1)
	EndIf
EndEvent 

The idea is that once the NPC gets far enough away from the player or the player changes cell the NPC would be deleted and the global variable used to count the total number of NPC would be reduced by the number of NPCs that were deleted.

 

This part of the script is definitely running as the global counter is decreasing, allowing more NPCs to be spawned, but spiralling out of control because none of them are being deleted.

 

There is a second part to the script that periodically moves the NPC close to the player if there is over a certain distance between them (I know this seems the opposite of the first part of the script, but the idea was to keep the NPC close to the player unless the player moves too quickly away or changes cell) and I'm not sure if this is preventing the NPC from being deleted or it's something else.

 

Any help would be appreciated.

 

Thanks,

 

WIGWAM

 

 

Link to comment
Share on other sites

The simple approach to this is to stuff any and all spawned NPCs into a quest ReferenceCollectionAlias and attach a simple script that:

Event OnUnload(ObjectReference akSender)
   akSender.Disable()
   akSender.Delete()
   Self.RemoveRef(akSender)
EndEvent

This is the pure joy of a ReferenceCollectionAlias because its scripts receive all of the Actor and ObjectReference events from its members. Zero extra RegisterForremoteEvent type effort or counters needed. 100% effective on all SKK spawning mods.

 

Edit: if you want to persist with form attached scripts and counters, an actor base script is unlikely to delete offspring ObjectReferences so you probably need to cast (Self as ObjectReference).Disable() - never used this method so cant say for sure.

Edited by SKK50
Link to comment
Share on other sites

  • Recently Browsing   0 members

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