dwesterwick / SPTQuestingBots

Mod for SPT that adds a questing system to bots and spawns PMC's and PScavs to mimic live Tarkov
47 stars 13 forks source link

do not recreate components if they already exist #3

Closed belettee closed 4 months ago

belettee commented 4 months ago

Hey Dan, thank you for your work, I'm a huge fan of the mod!

I'm running the mod in a setting where BotsController.AddActivePLayer will be called multiple times, but the code doesn't expect that at the moment and throws an error. If you're open to it, I'd like to use GetOrAddComponent instead of AddComponent in places where you're looking to have only one instance of a component. AddActivePlayerPatch then becomes idempotent.

Explanation of the error: QB's LocationData component is created when BotsController.AddActivePLayer is called. We need to call that function several times, which calls the BotHiveMindMonitor constructor multiple times, which tries to add the same key to the static BotHiveMindMonitor.sensors multiple times. This patch makes it so we do not create LocationData and other components again if they already exist.

Stacktrace of the error:

EXCEPTION: System.ArgumentException: An item with the same key has already been added. Key: InCombat
  at System.Collections.Generic.Dictionary`2[TKey,TValue].TryInsert (TKey key, TValue value, System.Collections.Generic.InsertionBehavior behavior) [0x000c1] in <eae584ce26bc40229c1b1aa476bfa589>:0 
  at System.Collections.Generic.Dictionary`2[TKey,TValue].Add (TKey key, TValue value) [0x00000] in <eae584ce26bc40229c1b1aa476bfa589>:0 
  at SPTQuestingBots.BotLogic.HiveMind.BotHiveMindMonitor..ctor () [0x00019] in <fbf3b433c40a4cb485ef1d651ee9eb6b>:0 
2024-04-26 23:38:39.098 +02:00|0.14.1.3.29351|Error|Default|ArgumentException: An item with the same key has already been added. Key: InCombat
System.Collections.Generic.Dictionary`2[TKey,TValue].TryInsert (TKey key, TValue value, System.Collections.Generic.InsertionBehavior behavior) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.Collections.Generic.Dictionary`2[TKey,TValue].Add (TKey key, TValue value) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
SPTQuestingBots.BotLogic.HiveMind.BotHiveMindMonitor..ctor () (at <fbf3b433c40a4cb485ef1d651ee9eb6b>:0)
UnityEngine.DebugLogHandler:LogException(Exception, Object)
LogHandler:UnityEngine.ILogHandler.LogException(Exception, Object)
UnityEngine.GameObject:AddComponent()
SPTQuestingBots.Components.LocationData:Awake()
UnityEngine.GameObject:AddComponent()
SPTQuestingBots.Patches.AddActivePlayerPatch:PatchPostfix()
EFT.BotsController:DMD<EFT.BotsController::AddActivePLayer>(BotsController, Player)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
dwesterwick commented 4 months ago

This should be no problem to implement, and thanks for submitting the PR. Once the SPT hub is accessible, I'll post the 0.5.0 release, and then I'll include this PR into the 0.5.1 release in a couple weeks.

dwesterwick commented 4 months ago

@belettee Not sure if you get automatic emails for releases now, but your PR is included in the new alpha build: https://github.com/dwesterwick/SPTQuestingBots/releases/tag/0.5.1-alpha1

Hope everything works for you! Thanks again for the PR!