Jump to content

Duplicates in containers


Recommended Posts

Is there a code snippet that I can be pointed at that will help me identify duplicate items in a container? I don't want to reinvent the wheel, but I don't want anyone else to either, so only already existing code:)

I'm looking to go through a container full of books and remove duplicates back to another container or the player.

 

diziet

Link to comment
Share on other sites

With a script on the container we can remove excess items as added when there is already at least one present. Non-SKSE

Some sort of trigger (hotkey, spell or activator object) with a script attached could be used to scan the container's items and remove extra down to one remaining. Definitely SKSE.

 

As far as existing code, pretty sure there is similar stuff but I do not know of anything that would specifically match up to what you want.

 

No wheel reinventing going on but rather figuring out how many spokes you want and how far apart to space them...

 

EDIT: There would be a non-SKSE way to do the second idea but it requires a "hack" by using a second container using the first idea and a stack dump potential transfer of all items from the first container into the second and using that second container to send back just one of each object.

Edited by IsharaMeradin
Link to comment
Share on other sites

Using SKSE, I have the following so far:

ScriptName	dz_move_duplicates_script	Extends	ObjectReference

Actor	Property	PlayerRef	Auto

Event OnActivate(ObjectReference akItemRef)												;akItemRef will be a container
	dz_move_dupes(akItemRef, PlayerRef)
EndEvent

Function dz_move_dupes(ObjectReference oldcontainer, ObjectReference newcontainer)
	Int NumItems = oldcontainer.GetNumItems()											;number of items in container
	While NumItems > 0																	;for each item...
		NumItems -= 1
		Form Item = GetNthForm(NumItems)
		Int ItemCount = GetItemCount(Item)												;...get the number(count) of that item
		If ItemCount > 1																;if there are duplicates...
			Int newItemCount = ItemCount - 1											;...how many to remove
			oldcontainer.RemoveItem(Item, newItemCount, False, newContainer)
		EndIf
	EndWhile
EndFunction

I envisage a hotkey activation on a container so the event part will change as this will possibly become part of a larger MCM menu script.

My use case in mind is a collection of books, so items that are tempered and or enchanted wouldn't be an issue, so do I need more?

 

diziet

 

Link to comment
Share on other sites

Anyone reading this, I made a mistake in the script, the two lines:

Form Item = GetNthForm(NumItems)
Int ItemCount = GetItemCount(Item)

 

should read:

Form Item = oldcontainer.GetNthForm(NumItems)
Int ItemCount = oldcontainer.GetItemCount(Item)

 

I didn't notice because it compiled!

 

diziet

Link to comment
Share on other sites

  • Recently Browsing   0 members

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