sw5e-foundry / sw5e

SW5e System for Foundry VTT
GNU General Public License v3.0
43 stars 30 forks source link

Issue with character import #707

Closed zeldafreak6245 closed 1 year ago

zeldafreak6245 commented 1 year ago

Source: Discord OP: Tastyhaggis Date: 06/25/2023

Describe the bug Hi all! Whenever I import characters from the 5E website something odd happens on the VTT. The first character tends to go through fine and it pulls through their classes and stats wonderfully.

Subsequent characters however have an issue where they are reimported and the prompt for advancement includes the PREVIOUS character's classes as well as their own.

To Reproduce

  1. Import first character without any issue. In this example a FIGHTER. Advancement allows the selection/processing of level upgrades.
  2. Import second character; a BERSERKER. When importing the advancement prompt appears.
  3. Berserker has extra steps which turns out the FIGHTER and BERSERKER advancements have complied meaning rather than a level one BERSERKER they are both a FIGHTER and BERSERKER.
  4. Subsequent imports compound the issue by adding levels from the previous classes.

Expected behavior Importing to work properly.

**Foundry Information Foundry version: V10, V11 SW5e version: 2.4.7

afwolfe commented 1 year ago

I am also encountering this in the 2.2.2 beta on Foundry V11. I did some testing and this appears to be related to the static variables in the CharacterImporter class:

https://github.com/sw5e-foundry/sw5e/blob/master/module/character-importer.mjs#L7

  static _itemsWithAdvancement = [];
  static _actor = null;

When importing multiple characters, the _itemsWithAdvancement is not cleared and applies the advancements of any previously imported characters. The _actor appears to be updated with the new Actor before any other methods using it are called, but could have some strange behaviors if the user starts another import before the current one is finished.

I would be interested in fixing this but would like to discuss the approach. Here are two thoughts I had, but there may be a better solution:

Ikaguia commented 1 year ago

They were local variables before, and I changed them to static to simplify the code. (which in hindsight was not that good of an idea) I believe the best way would actually be to change the variables and all functions to be instance variables and functions, also changing the code that calls the importer to create a new instance when doing so. This would solve the issue of advancements stacking, and also any other issues related to starting a second import while the first is underway

afwolfe commented 1 year ago

I agree that approach seems more appropriate and I can give it a try. I had started thinking through what passing in the objects as arguments and mutating them would look like and it was becoming quite ugly.