I am having very weird effects from a script and am trying to debug it, I was hoping someone can help.
What the script did originally was remove all the player's items into a box on first load, and then move them back to the player, thereby registering the re-added items through an onItemAdded() event to set up my item tracking array. Everything worked fine, but I realized the player was then left naked. So I added functionality to the script to track all the player's currently equipped items before removing everything, so that I could re-equip those items when the process was over.
Now what has happened is that about 75% of the time it works great, and everything gets re-equipped. But the other 25% of the time, one or more items DONT get re-equipped for some reason. The items that aren't re-equipped are always the items with the highest armor slot numbers, so there must be an issue with some of the later items not being properly inserted into the equippedItemsArray when I check the slots through my loop. I have a lock on it so I think it should be working right.
I started wondering whether I was changing states too quickly right after my registerEquippedItems function ended, since the new state didn't have the onItemUnequipped() event that picked up the items I was unequipping right at the end of that function, but removing the state change didn't solve the problem, I am still left bootless sometimes while my weapon, helmet, and armor get re-equipped.
Here is the relevant section of code, any suggestions would be greatly appreciated! The empty state just includes an onItemAdded() event that tracks items added, but isn't really relevant to the problem here.
...Actor property playerRef autoContainer property EA_QuarantineBarrel autoForm[] equippedItemsArray ;used during first boot initialization to keep track of what was equippedint equipNum = 0bool equipLock = falseEvent onInit() registerForSingleUpdate(0.0)EndEventEvent onUpdate() GoToState("registerEquipment") registerEquippedItems() GoToState("suspendTracking") ObjectReference ghostBox = PlayerRef.placeAtMe(EA_QuarantineBarrel) PlayerRef.RemoveAllItems(ghostBox, true) int totalEquipped = equipNum ;return and re-equip worn items first while equipNum > 0 ; equipNum -= 1 ; V ghostBox.removeItem(equippedItemsArray[equipNum], 1, true, playerRef) ; V playerRef.equipItem(equippedItemsArray[equipNum], false, true) ; V endWhile ; GoToState("") ;waiting for items to equip one by one while threading through the while totalEquipped > 0 ;onItemAdded event was taking too long, so I have opted to re-equip totalEquipped -= 1 ;everything immediately and then call onItemAdded on the equipped onItemAdded(equippedItemsArray[totalEquipped], 1, none, none) ;items afterward, greatly improving the visual speed of re-equip. endWhile ghostBox.RemoveAllItems(PlayerRef) ;return all other items to player [triggering onItemAdded() tracker] ghostBox.Disable() ghostBox.Delete()EndEventfunction registerEquippedItems() equippedItemsArray = new form[32] ;large enough to accommodate armor slotmasks 0-30 & two hand-equipped items ;CHECK LEFT HAND int leftType = playerRef.getEquippedItemType(0) ;no left weapon can't re-equip left hand weapon without SKSE if (leftType == 10) ; shield equippedItemsArray[equipNum] = playerRef.getEquippedShield() as form equipNum += 1 endif ;CHECK RIGHT HAND int rightType = playerRef.getEquippedItemType(1) if (rightType >= 1 && rightType <= 7) || rightType == 12 ;weapon/crossbow equippedItemsArray[equipNum] = playerRef.getEquippedWeapon() as form equipNum += 1 endif ;CHECK ALL ARMOR SLOTS int slotCount = 30 while slotCount < 61 playerRef.unEquipItemSlot(slotCount) slotCount += 1 endWhileendFunctionState registerEquipment Event OnObjectUnequipped(Form akBaseObject, ObjectReference akReference) while equipLock utility.wait(0.04) endWhile equipLock = true equippedItemsArray[equipNum] = akBaseObject equipNum += 1 equipLock = false EndEvent ;these following two empty events were added to this state during debug and the goToState("suspendTracking") was commented out, ;rather than going to the suspendTracking state that included them (because it didn't include OnObjectUnequipped()) Event onItemAdded(Form akBaseItem, int aiItemCount, ObjectReference akItemReference, ObjectReference akSourceContainer) EndEvent Event onItemRemoved(Form akBaseItem, int aiItemCount, ObjectReference akItemReference, ObjectReference akDestContainer) EndEventEndState;State EMPTY Event OnObjectUnequipped(Form akBaseObject, ObjectReference akReference) EndEvent;EndState