[Tutorial] How to fix the vanishing merchant stock bug
Posted: Sun Jan 22, 2023 3:43 pm
The vanishing merchant stock bug can occur with summonable merchants and merchant followers.
Sometimes these NPCs can loose their connection to their merchant chest. I experienced this during the development of SESM and the Dremora Merchant Tweak (old and deleted mod; I'm working on a rewrite with more features). Regarding the merchants of SESM, they couldn't access the chest containing the stock (leveled items, potions, armors, etc.) anymore after they became followers and the player entering another cell. If you reload a save and summon Dragonborn's Dremora Merchant, the merchant will not have any gold or items in his stock. Resummoning him solves the problem temporarily.
I solved both problems by implementing "invisible" middlemen. The middlemen are NPCs in a remote (interior) cell. They will trade with the player on behalf of the original merchants using the default merchant factions and without any follower factions. They are named after the original merchants so that the player will not notice the workaround .
Regarding the Dremora Merchant, I duplicated the NPC, the merchant faction and the merchant chest. Then I put the duplicated NPC (who is the middleman) and the duplicated chest into the remote cell. I replaced the merchant faction of the duplicated NPC with the duplicated merchant faction and let the duplicated faction point to the duplicated merchant chest. I also removed the gold from the chest as I add it to the middleman via script.
I modified the trading dialogue script fragment:
I commented the ShowBarterMenu() part out and let the script fragment point to the custom Barter() function in the quest script (GetOwingQuest() part)
The original quest script with some additional annotations:
You can ignore most of the script as only the last line of the custom Barter() function and the second and third properties are important.
The shortened version of the quest script:
I attached he quest script to the quest DLC2Init which contains the dialogue of the Dremora Merchant.
The middleman will never leave the cell with his merchant chest and so he will never loose the connection to the chest and the stock.
Sometimes these NPCs can loose their connection to their merchant chest. I experienced this during the development of SESM and the Dremora Merchant Tweak (old and deleted mod; I'm working on a rewrite with more features). Regarding the merchants of SESM, they couldn't access the chest containing the stock (leveled items, potions, armors, etc.) anymore after they became followers and the player entering another cell. If you reload a save and summon Dragonborn's Dremora Merchant, the merchant will not have any gold or items in his stock. Resummoning him solves the problem temporarily.
I solved both problems by implementing "invisible" middlemen. The middlemen are NPCs in a remote (interior) cell. They will trade with the player on behalf of the original merchants using the default merchant factions and without any follower factions. They are named after the original merchants so that the player will not notice the workaround .
Regarding the Dremora Merchant, I duplicated the NPC, the merchant faction and the merchant chest. Then I put the duplicated NPC (who is the middleman) and the duplicated chest into the remote cell. I replaced the merchant faction of the duplicated NPC with the duplicated merchant faction and let the duplicated faction point to the duplicated merchant chest. I also removed the gold from the chest as I add it to the middleman via script.
I modified the trading dialogue script fragment:
Code: Select all
Scriptname DLC2_TIF__0201EEAC Extends TopicInfo Hidden
Function Fragment_0(ObjectReference akSpeakerRef)
Actor akSpeaker = akSpeakerRef as Actor
(GetOwningQuest() as SESM_Dremora_Merchant_Tweak).Barter()
;akSpeaker.ShowBarterMenu()
EndFunction
The original quest script with some additional annotations:
Code: Select all
Scriptname SESM_Dremora_Merchant_Tweak extends Quest
SESMvariablesStorageScript property vars auto
ObjectReference Property MerchantChest auto
Actor Property Middleman Auto
; used properties from SESMvariablesStorageScript:
; GlobalVariable Property MerchantGold Auto
; MiscObject Property gold Auto
; GlobalVariable Property MerchantRestock Auto
Function Barter()
MerchantChest.removeitem(vars.gold, MerchantChest.getitemcount(vars.gold))
Middleman.removeitem(vars.gold, Middleman.getitemcount(vars.gold))
If (vars.MerchantRestock.GetValueInt() == 2)
MerchantChest.reset()
endif
Middleman.additem(vars.gold, vars.MerchantGold.getvalueint())
Middleman.showbartermenu()
EndFunction
The shortened version of the quest script:
Code: Select all
Scriptname SESM_Dremora_Merchant_Tweak extends Quest
ObjectReference Property MerchantChest auto ; the duplicated merchant chest in the remote cell
Actor Property Middleman Auto ; the duplicated NPC in the remote cell
Function Barter()
Middleman.showbartermenu()
EndFunction
The middleman will never leave the cell with his merchant chest and so he will never loose the connection to the chest and the stock.