Jump to content

[PSA] How to safely .Delete() powered references with SUP F4SE


niston

Recommended Posts

Use this function:

; uses functions from SUP
Import SUP_F4SE

; Assign this in CK or use Game.GetForm() or whatever
Keyword Property WorkshopItemKeyword Auto Const Mandatory

; Safely delete a powered reference from script with SUP F4SE
; Parameters
; refElement: Powered ObjectReference to delete
; Return Value
; True = success, False = failure
Bool Function SafeDeletePoweredRef(ObjectReference refElement)
    ; parameters supplied?
    If (refElement == none)
        Debug.Trace(Self + ": ERROR - SafeDeletePoweredRef() failed: Missing or invalid parameter.")
        Return False
    EndIf
    
    ; sup scrap crash fix active?
    If (IsSUPPatchActive(1) == false)                
        Debug.Trace(Self + ": ERROR - SafeDeletePoweredRef() failed: SUP F4SE Scrap Crash Fix disabled; Unable to safely delete reference.")
        Return False
    EndIf
    
    ; get refElement workshop
    ObjectReference refWorkshop = refElement.GetLinkedRef(WorkshopItemKeyword) as ObjectReference
    If (refWorkshop == none)
        Debug.Trace(Self + ": ERROR - SafeDeletePoweredRef() failed: Object does not have valid WorkshopItemKeyword reflink.")
        Return False
    EndIf
	
    ; get objects connected to refElement
    ObjectReference[] connectedObjects = refElement.GetConnectedObjects()

    ; delete
    refElement.Delete()
            
    ; notify formerly connected objects
    Int i = 0
    While (i < connectedObjects.Length)
        WorkshopGenerateFakeReferenceEvent(refWorkshop, connectedObjects[i])
        i += 1
    EndWhile
    
    ; success
    Return True
EndFunction

That's all - No need to open Workshop or some suchlike; It won't CTD.

  • Thanks 1
Link to comment
Share on other sites

  • Recently Browsing   0 members

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