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] Token HUD not shown, `game.tokenActionHud is undefined` in console #143

Closed hamishcampbell closed 1 year ago

hamishcampbell commented 1 year ago

Describe the bug

Token HUD not visible.

Steps to reproduce

  1. Install token HUD core & 5e
  2. Enable for game world
  3. Nothing happens

Pressing H causes the following in console (Firefox):

Uncaught TypeError: game.tokenActionHud is undefined
    onDown token-action-hud-core.min.js:1759
    _executeKeybind foundry.js:2500
    _processKeyboardContext foundry.js:2541
    _handleKeyboardEvent foundry.js:2599
    _activateListeners foundry.js:2225
    _activateListeners foundry.js:2225
    initializeKeyboard foundry.js:8850
    setupGame foundry.js:8659
    _initializeGameView foundry.js:9925
    _initializeView foundry.js:9901
    initialize foundry.js:8588
    async* foundry.js:90285
    async* foundry.js:90266

Interestingly, if I break there are run game.initialize() the HUD now appears and begins working, although with some hooks (e.g. show/hide on H) running twice. So it sounds like it's not booting correctly, but some hooks are registered but the instance isn't attached to game correctly.

Versions:

hamishcampbell commented 1 year ago

Running Hooks.callAll('tokenActionHudCoreReady') in console appears to cleanly boot it.

Larkinabout commented 1 year ago

I've noticed this when Foundry doesn't load straight into a scene. It's because the hooks are nested and the HUD is expecting a canvas. Is this true for your world?

hamishcampbell commented 1 year ago

I'm brand-new to Foundry hooks, but looking through the code - it seems that:

  1. The 'ready' hook triggers tokenActionHudCoreApiReady: https://github.com/Larkinabout/fvtt-token-action-hud-core/blob/c27f1b77f9d91c1628510cb54b79aa13647eb7bc/scripts/init.js#L36
  2. this triggers the specific module, which triggers tokenActionHudSystemReady: https://github.com/Larkinabout/fvtt-token-action-hud-dnd5e/blob/64eae03322c3da765149c441dc9a41c9681e84c1/scripts/init.js#L10
  3. which is supposed to trigger tokenActionHudCoreReady: https://github.com/Larkinabout/fvtt-token-action-hud-core/blob/c27f1b77f9d91c1628510cb54b79aa13647eb7bc/scripts/init.js#L72

However tokenActionHudCoreReady isn't registered until canvasReady is run, and this appears to happen after the above - or at least it is in my configuration.

It seems to me that tokenActionHudCoreReady should be registered outside of canvasReady: https://github.com/Larkinabout/fvtt-token-action-hud-core/blob/c27f1b77f9d91c1628510cb54b79aa13647eb7bc/scripts/init.js#L76. Currently, it doesn't exist at the time the module triggers tokenActionHudCoreReady so nothing happens and I don't quite understand why it should happen on every canvasReady. E.g. canvasReady is called on each scene load, so the hook is presumbly registered multiple times over the lifetime of a session, which doesn't seem right?

hamishcampbell commented 1 year ago

I've noticed this when Foundry doesn't load straight into a scene. It's because the hooks are nested and the HUD is expecting a canvas. Is this true for your world?

It is! And yes if I activate a scene and reload the issue goes away. Thanks, that's a useful workaround.

Larkinabout commented 1 year ago

Good to hear. I'll look at using a different sequence of hooks. It was nested that way to avoid triggering the code before Foundry VTT was ready, but it has this unintended consequence.

hamishcampbell commented 1 year ago

Thanks. tldr; for future visitors: if the HUD doesn't appear to load correctly, make sure you have an initial scene set.

Larkinabout commented 1 year ago

Fixed in Token Action HUD Core 1.4.9.