Error After Loading To Another Space

Hi!

I’ve taken a look at the space switching logic and it looks like there might have been an invalid cursor which could be the source of the issue. Here is the fixed version:

import * as ecs from '@8thwall/ecs'

ecs.registerComponent({
  name: 'Load Space on Click',
  schema: {
    spaceName: ecs.string,
    hideUIOnClick: ecs.boolean,
    hideInventoryOnClick: ecs.boolean,
    inventoryUI: ecs.eid,
    inventoryUIButton: ecs.eid,
    inventoryUiClosebutton: ecs.eid,
  },
  schemaDefaults: {
    spaceName: 'World Map',
    hideUIOnClick: false,
    hideInventoryOnClick: false,
  },
  data: {
    isLoading: ecs.boolean,
  },
  stateMachine: ({world, eid, schemaAttribute, dataAttribute}) => {
    ecs.defineState('default')
      .initial()
      .onEnter(() => {
        dataAttribute.set(eid, {isLoading: false})
      })
      .listen(eid, ecs.input.UI_CLICK, () => {
        const {hideUIOnClick, hideInventoryOnClick, inventoryUI, inventoryUIButton, inventoryUiClosebutton} = schemaAttribute.get(eid)

        if (hideUIOnClick) {
          window.dispatchEvent(
            new CustomEvent('back-to-map', {detail: {}})
          )
          console.log('[Back to map]: event fired')
        }

        if (hideInventoryOnClick) {
          console.log('disable buttons')
          ecs.Disabled.set(world, inventoryUI)
          ecs.Disabled.remove(world, inventoryUIButton)
          ecs.Disabled.remove(world, inventoryUiClosebutton)
          ecs.Disabled.set(world, eid)
        }
      })
      .listen(eid, ecs.input.UI_RELEASED, () => {
        const {spaceName} = schemaAttribute.get(eid)
        const state = dataAttribute.cursor(eid)
        if (spaceName && !state.isLoading) {
          state.isLoading = true
          console.log('queued space load', spaceName)

          world.time.setTimeout(() => {
            try {
              console.log('loading space', spaceName)
              world.spaces.loadSpace(spaceName)
            } catch (e) {
              console.error('failed to load space', e)
            } finally {
              dataAttribute.set(eid, {isLoading: false})
            }
          }, 1000)
        }
      })
  },
})

Also you should always use world.time to set a timeout instead of using the Javascript native timeout.