Scriptname _00E_TorchControl Hidden

Int Function _GetHandSlotEx(int iHand) global
	; By default, iHand = 0 for left hand, 1 for right hand
	; In SKSE's "Equip Ex" functions though: iHand = 2 for left hand, 1 for right hand
	Return 2 - iHand
EndFunction

Bool Function UnequipTorches(Form[] equippedTorches) global
	Actor PlayerREF = Game.GetForm(0x14) as Actor
	Bool unequipping = False
	Int iHand = 0
	While iHand < equippedTorches.Length
		If PlayerREF.GetEquippedItemType(iHand) == 11 ; A torch is in the hand?
			Form torch = PlayerREF.GetEquippedObject(iHand)
			If torch
				PlayerREF.UnequipItemEx(torch, _GetHandSlotEx(iHand))
				equippedTorches[iHand] = torch
				unequipping = True
			EndIf
		EndIf

		iHand += 1
	EndWhile

	Return unequipping
EndFunction

Function ReequipTorches(Form[] equippedTorches) global
	Actor PlayerREF = Game.GetForm(0x14) as Actor
	Int iHand = 0
	While iHand < equippedTorches.Length
		Form torch = equippedTorches[iHand]
		If torch
			If PlayerREF.GetItemCount(torch) > 0
				PlayerREF.EquipItemEx(torch, _GetHandSlotEx(iHand), False, True)
			EndIf
			; "Forget" about the form to not keep it persistent if equippedTorches is a script's property or variable
			equippedTorches[iHand] = None 
		EndIf
		iHand += 1
	EndWhile
EndFunction