Larkinabout / fvtt-token-action-hud-core

Token Action HUD is a repositionable HUD of actions for a selected token.
13 stars 18 forks source link

[BUG] tokenActionHudCoreApiReady is fired off too early. #161

Closed dmrickey closed 1 year ago

dmrickey commented 1 year ago

One of the pf1 system devs was testing with v11 and ran into this issue during startup.

hud-builder.js:238 Uncaught (in promise) TypeError: undefined. Cannot read properties of undefined (reading 'TurnEnd') [Detected 2 packages: token-action-hud-pf1, token-action-hud-core] at #_buildCombat (hud-builder.js:238:61) at HudBuilder.buildSystemActions (hud-builder.js:31:31) at #l (token-action-hud-core.min.js:1:18893) at HudBuilder.buildHud (token-action-hud-core.min.js:1:17135) at async TokenActionHud._updateHud (token-action-hud-core.min.js:1:76637)

Looking into it, it's me accessing game.i18n.translations.COMBAT.TurnEnd while the HUD is being built. I'm not sure how he got a token selected during startup, but it seems like that must be firing off before the foundry ready hook which doesn't seem right.

Larkinabout commented 1 year ago

tokenActionHudCoreApiReady is called from within the ready hook. When is game.i18n.translations.COMBAT.TurnEnd defined?

dmrickey commented 1 year ago

I'm not sure, it's a core string. There is a separate i18nInit hook. But I would assume that should have already been fired by the time ready is fired

Larkinabout commented 1 year ago

The expected sequence is:

  1. ready hook is called.
  2. TAH Core calls the tokenActionHudCoreApiReady hook within a ready hook call handler.
  3. TAH system module registers its extended classes and calls the tokenActionHudSystemReady hook within tokenActionHudCoreApiReady hook call handlers.
  4. TAH initialises the HUD within a tokenActionHudSystemReady hook call handler.

I've not had any issue with game.i18n.localize calls within the build phase, but I've not used game.i18n.translations anywhere.

dmrickey commented 1 year ago

Calling game.i18n.localize seems to work fine. This is specifically trying to use a core foundry value that's already been pre-localized and cached. As far as I know this is how I should be accessing strings localized by foundry

dmrickey commented 1 year ago

I've swapped it to game.i18n.localize('COMBAT.TurnEnd') which apparently is a correct way to fetch this specific string. I imagine this will work as intended and the problem I was running into was that even though localization was ready, fetching it directly wasn't working because it hadn't been cached by anything yet so that specific value wasn't directly available.