Scriptname RenScrap:RenScrapMachineScript extends ObjectReference

Formlist Property RenComponents Auto
Formlist Property RenCompMiscObj Auto
Formlist Property WorkshopMenu01Resource Auto
ObjectReference Property PlayerRef Auto

Container Property RenTempContainer Auto

Event OnInit()
	Self.RegisterForMenuOpenCloseEvent("ContainerMenu")
	PlayerRef = Game.GetPlayer()
endEvent

Event OnMenuOpenCloseEvent(string asMenuName, bool abOpening)
	int itr = 0
	int itrtwo = 0
	ObjectReference tempcontainer = Self.PlaceAtMe(RenTempContainer, 1)
	while (tempcontainer.Is3DLoaded() == false)
		; spin...
	endWhile
	Self.RemoveAllItems(tempcontainer)		
	if (tempcontainer.GetItemCount() > 0 && asMenuName == "ContainerMenu" && abOpening == false)
		bool continue = true
		int itemcount = tempcontainer.GetItemCount()
		while (tempcontainer.GetItemCount() > 0)
			debug.notification("Items left: " + tempcontainer.GetItemCount())	
			while (itrtwo < RenComponents.GetSize())
				Component currcomp = RenComponents.GetAt(itrtwo) As Component
				tempcontainer.RemoveComponents(currcomp, 1)
				tempcontainer.RemoveItem(RenCompMiscObj, -1, false, PlayerRef)
				tempcontainer.RemoveItem(RenComponents, -1, false, PlayerRef)				
				itrtwo += 1
			endWhile
			itrtwo = 0
			itr += 1
		endWhile
	endif
	tempcontainer.RemoveAllItems(PlayerRef)
	; just in case.	
	tempcontainer.Disable()
	tempcontainer.DeleteWhenAble()
	Self.RemoveAllItems(PlayerRef)
endEvent

