From 88203da76229a6083539538c208139106b616d45 Mon Sep 17 00:00:00 2001 From: podar <1999688+podarsmarty@users.noreply.github.com> Date: Wed, 18 Sep 2024 10:12:16 -0500 Subject: [PATCH] [Bug] Fixing crash that happens when failing to load experimental sprites. --- src/field/pokemon.ts | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/field/pokemon.ts b/src/field/pokemon.ts index cdafc960382e..9655e1b1d5b3 100644 --- a/src/field/pokemon.ts +++ b/src/field/pokemon.ts @@ -436,18 +436,33 @@ export default abstract class Pokemon extends Phaser.GameObjects.Container { if (variantColorCache.hasOwnProperty(key)) { return resolve(); } - this.scene.cachedFetch(`./images/pokemon/variant/${useExpSprite ? "exp/" : ""}${battleSpritePath}.json`). - then(res => { + const getSpritePath = (isExpSprite: boolean): string => { + return `./images/pokemon/variant/${isExpSprite ? "exp/" : ""}${battleSpritePath}.json`; + }; + const handleError = (spritePath: string, isExpSprite: boolean, ...optionalParams: any[]) => { + console.warn(`Could not load ${spritePath}!`, ...optionalParams); + if (isExpSprite) { + // If failed to load experimental sprites, fall back on non-experimental before giving up + attemptProcessVariant(false); + } + return; + }; + const attemptProcessVariant = (isExpSprite: boolean) => { + const spritePath = getSpritePath(useExpSprite); + this.scene.cachedFetch(spritePath).then(res => { // Prevent the JSON from processing if it failed to load if (!res.ok) { - console.error(`Could not load ${res.url}!`); - return; + handleError(res.url, isExpSprite); } return res.json(); + }).catch(error => { + handleError(spritePath, isExpSprite, error); }).then(c => { variantColorCache[key] = c; resolve(); }); + }; + attemptProcessVariant(useExpSprite); } else { resolve(); }