Jump to content

[LE] Need help speeding up my script


Recommended Posts

Not sure what I'm doing wrong, but I still can't get the perk script to compile without errors. It seems to be stemming from the CK not recognizing the property for the other script:

unknown type cp_ingredientswap (I get this error from the "CP_IngredientSwap Property PlayerAlias Auto" line)
cp_ingredientswap is not a known user-defined type (I get this error twice, once for each time the SwapInventory() function is called)
Edited by candlepin
Link to comment
Share on other sites

Make sure the sourcefile "CP_IngredientSwap.psc" exists in the right folder "data\scripts\source" and was compiled successful before perk fragment.

 

You can also try this:

  ReferenceAlias PROPERTY PlayerAlias auto    ; playeralias which has attached the script we want to swap

within perk fragment use that:

  (PlayerAlias as CP_IngredientSwap).SwapInventory(TRUE)

..

  (PlayerAlias as CP_IngredientSwap).SwapInventory(False)

maybe it's useful.

Edited by ReDragon2013
Link to comment
Share on other sites

I tried to make the content of both scripts provided by foamyesque, imho a bit more clean.

 

CP_IngredientSwap

 

Scriptname CP_IngredientSwap extends ReferenceAlias

; https://forums.nexusmods.com/index.php?/topic/6580351-need-help-speeding-up-my-script/page-3
; foamyesque wrote: "Well, until I can diagnose why I get two OnItemRemoved events, these are my scripts:"

  GlobalVariable PROPERTY CP_SwapState auto
  Perk  PROPERTY CP_AlchemyPerk          auto
  Spell PROPERTY CP_TransmuteIngredients auto

  FormList PROPERTY CP_IngredientList auto
  FormList PROPERTY CP_OriginalList   auto
  FormList PROPERTY CP_EmptyList      auto Hidden    ; not filled formlist
  FormList PROPERTY CP_ItemStack      auto Hidden    ; filled by OnItemAdded(), OnItemRemoved()

  ReferenceAlias PROPERTY SwapContainerAlias auto
  ObjectReference SwapContainerRef

  Bool bUpdating
  Bool bSwapping

 ;Int  iSKSEVersion
 ;ObjectReference PlayerRef


;-------------------------
FUNCTION myF_Init(Float f)
;-------------------------
;;;    iSKSEVersion = SKSE.GetVersion()

    actor aRef = Game.GetPlayer()
    self.ForceRefTo(aRef as ObjectReference)        ; should not be necessary, but who knows
    aRef.AddPerk(CP_AlchemyPerk)                    ; give player the special perk
    aRef = None

    gotoState("Starting")            ; ### STATE ###
    RegisterForSingleUpdate(f)
ENDFUNCTION


; -- EVENTs -- 4 + "Starting" + "Started"

EVENT OnInit()
    myF_Init(5.0)
ENDEVENT

EVENT OnPlayerLoadGame()
    myF_Init(1.0)
ENDEVENT


EVENT OnItemAdded(Form akBaseItem, Int aiItemCount, ObjectReference akItemReference, ObjectReference akSourceContainer)
    UnregisterForUpdate()
    CP_ItemStack.AddForm(akBaseItem)            ; ADD 1
    RegisterForSingleUpdate(0.1)    
ENDEVENT


EVENT OnItemRemoved(Form akBaseItem, Int aiItemCount, ObjectReference akItemReference, ObjectReference akDestContainer)
    UnregisterForUpdate()
    CP_ItemStack.AddForm(akBaseItem)            ; ADD 2
    RegisterForSingleUpdate(0.1)
ENDEVENT


;======================================
State Starting
;=============
EVENT OnUpdate()
IF ( bUpdating )
    RETURN    ; - STOP - busy with swap
ENDIF
;-------------------
    bUpdating = TRUE        ; *T*

    SwapContainerRef = SwapContainerAlias.GetReference()

    SwapContainerRef.RemoveAllItems()
;IF (iSKSEVersion > 0)                            ; SKSE version
;    SeedInventory_SKSE(TRUE)
;ELSE
    SeedInventory(TRUE)
;ENDIF

    SwapContainerRef.RemoveAllItems()
;IF (iSKSEVersion > 0)                            ; SKSE version
;    SeedInventory_SKSE(False)
;ELSE
    SeedInventory(False)
;ENDIF

    bUpdating = False        ; ***

    gotoState("Started")            ; ### STATE ###
    OnUpdate()                                        ; enlarge stacksize by calling OnUpdate() directly in state "Started"

    SwapInventory( myF_GetSwapState(TRUE) )
ENDEVENT
;=======
endState


;======================================
State Started
;============
EVENT OnSpellCast(Form akSpell)
IF (akSpell == CP_TransmuteIngredients)
    SwapInventory( myF_GetSwapState(False) )
ENDIF
ENDEVENT

EVENT OnUpdate()
WHILE (bUpdating)
    Utility.Wait(0.1)        ; hold on, I am busy with update action
ENDWHILE
;--------------
    bUpdating = TRUE        ; *T*

    WHILE (CP_ItemStack.GetSize() > 0)
        myF_LookupItem( CP_ItemStack.GetAt(0) )
    ENDWHILE
;--------------
    bUpdating = False      ; ***
ENDEVENT
;-------
EndState


; -- FUNCTIONs -- 1 + 5

;---------------------------------------------
Bool FUNCTION myF_GetSwapState(Bool bOpposite)  ; internal helper to shrink stacksize
;---------------------------------------------
    bool bOK = CP_SwapState.GetValue() as Bool
IF ( bOpposite )
    RETURN !bOK
ENDIF
;---------
    RETURN bOK
ENDFUNCTION


;----------------------------------------------
FUNCTION myF_SwapItem(Form akBaseItem, Form fm)    ; internal helper to shrink stacksize
;----------------------------------------------
    int i = self.GetReference().GetItemCount(akBaseItem)    ; iCount from players inventory
    SwapContainerRef.RemoveItem(fm, 999999, TRUE)            ; fm = swapItem
    SwapContainerRef.AddItem(fm, i, TRUE)
ENDFUNCTION


;---------------------------------------
FUNCTION myF_LookupItem(Form akBaseItem)  ; internal only
;---------------------------------------
    CP_ItemStack.RemoveAddedForm(akBaseItem)    ; REMOVE

    int i = CP_OriginalList.Find(akBaseItem)            ; akLookupList_1

IF (i >= 0) && (i < CP_IngredientList.GetSize())        ; akSendList_1
    myF_SwapItem(akBaseItem, CP_IngredientList.GetAt(i))
    RETURN    ; - STOP -    /1
ENDIF
;=====================
    i = CP_IngredientList.Find(akBaseItem)                ; akLookupList_2

IF (i < 0)            
    RETURN    ; - STOP -    /2    nothing
ENDIF
;---------------------
IF (i >= CP_OriginalList.GetSize())                        ; akSendList_2
    Debug.Trace(self+" LookUpItem() - Formlists are of different size")
    RETURN    ; - STOP -    /3    error
ENDIF
;---------------------
    myF_SwapItem(akBaseItem, CP_OriginalList.GetAt(i))
    ;                    /4
ENDFUNCTION


;---------------------------------------
FUNCTION SeedInventory(Bool abSwapState)    ; called twice by OnUpdate() within state "Starting"
;---------------------------------------
    objectReference oRef = self.GetReference()

    int i
    int j
    int n

IF ( abSwapState )                    ; TRUE    --    lookup=CP_IngredientList, send=CP_OriginalList
    i = CP_IngredientList.GetSize()
    j = CP_OriginalList.GetSize()
    WHILE (i > 0)
        i = i - 1
        n = oRef.GetItemCount( CP_IngredientList.GetAt(i) )
        IF (n > 0) && (i < j)
            SwapContainerRef.AddItem(CP_OriginalList.GetAt(i), n)
        ENDIF
    ENDWHILE
ELSE                                ; False    --    lookup=CP_OriginalList, send=CP_IngredientList
    i = CP_OriginalList.GetSize()
    j = CP_IngredientList.GetSize()
    WHILE (i > 0)
        i = i - 1
        n = oRef.GetItemCount( CP_OriginalList.GetAt(i) )
        IF (n > 0) && (i < j)
            SwapContainerRef.AddItem(CP_IngredientList.GetAt(i), n)
        ENDIF
    ENDWHILE
ENDIF
ENDFUNCTION

;;--------------------------------------------
;FUNCTION SeedInventory_SKSE(Bool abSwapState)    ; called twice by OnUpdate() within state "Starting"
;;--------------------------------------------
;    objectReference oRef = self.GetReference()
;    form[] a                                        ; a = LookupArray
;    form[] b                                        ; b = SendArray

;    IF ( abSwapState )
;        a = CP_IngredientList.ToArray()
;        b = CP_OriginalList.ToArray()
;    ELSE
;        a = CP_OriginalList.ToArray()
;        b = CP_IngredientList.ToArray()
;    ENDIF

;    int i = a.Length
;    int j = b.Length
;    WHILE (i > 0)
;        i = i - 1
;        int n = oRef.GetItemCount(a[i])                ; players item count
;        IF (n > 0) && (i < j)
;            SwapContainerRef.AddItem(b[i], n)
;        ENDIF
;    ENDWHILE
;ENDFUNCTION


;----------------------------------------
FUNCTION SwapInventory(Bool abTargetSwap)    ; called external twice by "foam_PRKF_CP_AlchemyPerk_01001D94" and OnUpdate(), OnSpellCast()
;----------------------------------------
WHILE (bSwapping)
    Utility.Wait(0.1)        ; hold on, I am busy with swap action
ENDWHILE
;---------------
    bSwapping = TRUE    ; *T*

    self.RemoveAllInventoryEventFilters()
    self.AddInventoryEventFilter(CP_EmptyList)        ; empty formlist, do not call any event for OnItemAdded(), OnItemRemoved()

    objectReference oRef = self.GetReference()        ; PlayerRef

    IF ( abTargetSwap )
        oRef.RemoveItem(CP_OriginalList, 999999, TRUE, SwapContainerRef)
        SwapContainerRef.RemoveItem(CP_IngredientList, 999999, TRUE, oRef)
    ELSE
        oRef.RemoveItem(CP_IngredientList, 999999, TRUE, SwapContainerRef)
        SwapContainerRef.RemoveItem(CP_OriginalList, 999999, TRUE, oRef)    
    ENDIF

    self.RemoveAllInventoryEventFilters()
    self.AddInventoryEventFilter(CP_OriginalList)
    self.AddInventoryEventFilter(CP_IngredientList)

    CP_SwapState.SetValue(abTargetSwap as Float)    ; update globalVar

    bSwapping = False        ; ***
ENDFUNCTION

 

 

 

foam_PRKF_CP_AlchemyPerk_01001D94

 

;BEGIN FRAGMENT CODE - Do not edit anything between this and the end comment
;NEXT FRAGMENT INDEX 4
Scriptname foam_PRKF_CP_AlchemyPerk_01001D94 Extends Perk Hidden
; to override the standard activation and ensure the swaps are completed before the crafting menu opens:

;BEGIN FRAGMENT Fragment_0
Function Fragment_0(ObjectReference akTargetRef, Actor akActor)
;BEGIN CODE
IF ( bActivated )
    RETURN    ; - STOP -
ENDIF
;---------------------
    bActivated = TRUE                    ; Threadlock on
    PlayerAlias.SwapInventory(TRUE)

    akTargetRef.Activate(akActor as ObjectReference)
    Utility.Wait(0.25)                                        ; added by ReDragon, maybe useful

; https://www.creationkit.com/index.php?title=IsFurnitureInUse_-_ObjectReference

    bool bOK = TRUE
    WHILE (bOK)
        bOK = akTargetRef.IsFurnitureInUse(TRUE)
        bOK = bOK || !Game.IsLookingControlsEnabled()
        IF ( bOK )
            Utility.Wait(0.5)
        ENDIF
    ENDWHILE

    PlayerAlias.SwapInventory(False)
    bActivated = False                    ; Threadlock off
;END CODE
EndFunction
;END FRAGMENT
;END FRAGMENT CODE - Do not edit anything between this and the begin comment

  CP_IngredientSwap PROPERTY PlayerAlias auto    ; name of playeralias script we want to swap
  Bool bActivated                                ; [default=false]

 

 

Link to comment
Share on other sites

 

Not sure what I'm doing wrong, but I still can't get the perk script to compile without errors. It seems to be stemming from the CK not recognizing the property for the other script:

unknown type cp_ingredientswap (I get this error from the "CP_IngredientSwap Property PlayerAlias Auto" line)
cp_ingredientswap is not a known user-defined type (I get this error twice, once for each time the SwapInventory() function is called)

 

CP_IngredientSwap is the name of the 'extends ReferenceAlias' script. If you renamed that script to something else when you created it, you'll need to update the property declarations to use that name instead. You'll also want to make sure that the extends-ReferenceAlias script compiled and has the .pex file in your scripts directory, as RedDragon said.

Link to comment
Share on other sites

  • Recently Browsing   0 members

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