Studio Niantic: Issue Setting Parent for Entity from String in ECS

I can’t use the Eid extracted from the string (schema.thornBushsStr) to setParent. However, using an element stored in the thornBushArray works.

world.setParent(thornBushArray[0], schema.mainSystemId) ← It’s run
world.setParent(thornBush, schema.mainSystemId) ← It’s not run!

Here all my code in ObstacleManager.js script

import * as ecs from '@8thwall/ecs'
import {addCleanup, doCleanup} from './Utilities/cleanup'
import {getRandomValue, PushIntoString} from './Utilities/Utilities'
import {BlockController} from './BlockController'
import {Block} from './Block'

let startCreateObstacleId
const thornBushArray = []

function CreateThornBushPooling(world, schema) {
  for (let i = 0; i < 2; i++) {
    const thornBushId = world.createEntity()
    ecs.Position.set(world, thornBushId, {x: -1 + i * 2, y: 0.4, z: 0})
    world.setScale(thornBushId, 1, 1, 1)
    ecs.GltfModel.set(world, thornBushId, {
      url: 'assets/3DModel_ThornBush.glb',
    })
    thornBushArray.push(thornBushId)
    schema.thornBushsStr = PushIntoString(schema.thornBushsStr, thornBushId)
  }
}

const ObstacleManager = ecs.registerComponent({
  name: 'ObstacleManager',
  schema: {
    mainSystemId: ecs.eid,
    // For create thorn bush
    thornBushsStr: ecs.string,
    nextRowSpawnThorn: ecs.i32,
    eachNumRowToSpawnThorn: ecs.i32,
  },
  schemaDefaults: {
    // For create thorn bush
    thornBushsStr: '',
    nextRowSpawnThorn: 0,
    eachNumRowToSpawnThorn: 9,
  },
  add: (world, component) => {
    const {eid, schema, schemaAttribute} = component

    console.log(`Create ObstacleManger with mainsystem: ${schema.mainSystemId}`)

    CreateThornBushPooling(world, schema)

    // Wating about 400ms for create obstacle when blocks were create
    startCreateObstacleId = setTimeout(() => {
      const curSche = schemaAttribute.cursor(eid)
      if (BlockController.has(world, curSche.mainSystemId)) {
        const blockControl = BlockController.cursor(world, curSche.mainSystemId)
        const blockRowList = blockControl.blockRowsStr.split(' ')
        const rIndex = getRandomValue(2, blockRowList.length - 1)
        const randomRow = blockRowList[rIndex]
        const blocksInRow = Array.from(world.getChildren(randomRow))

        for (let i = 0; i < blocksInRow.length; i++) {
          const block = Block.cursor(world, blocksInRow[i])
          if (block.activeBlock) {
            const thornBush = curSche.thornBushsStr.split(' ')[0]
            const posX = ecs.Position.get(world, block.iD).x
            const posY = ecs.Position.get(world, randomRow).y + 0.38
            ecs.Position.set(world, thornBush, {x: posX, y: posY, z: 0})
            world.setParent(thornBushArray[0], schema.mainSystemId)
            world.setParent(thornBush, schema.mainSystemId) <- It's not run
            console.log(`child: ${thornBushArray[0]} | ${thornBush}, parent: ${randomRow}`)
            break
          }
        }
      }
    }, 400)

    // ---------------------------- DO CLEANUP EVENT ----------------------------
    const cleanup = () => {
      clearTimeout(startCreateObstacleId)
    }
    addCleanup(component, cleanup)
  },
  remove: (world, component) => {
    doCleanup(component)
  },
})

export {ObstacleManager}

I have tried printing the log with this line of code to check eid thornBush entity. But it’s the same eid!.
Code → console.log(child: ${thornBushArray[0]} | ${thornBush}, parent: ${randomRow})
Console → : child: 709 | 709, parent: 716

It looks like the issue arises because thornBush extracted from schema.thornBushsStr is a string, whereas world.setParent expects an entity ID (Eid), which is likely a BigInt. That’s why world.setParent(thornBushArray[0], schema.mainSystemId) works—because thornBushArray[0] is already an Eid.

To fix the problem, you need to convert the string thornBush into an Eid before passing it to setParent. You can do this using the BigInt function:

const thornBushStr = curSche.thornBushsStr.split(' ')[0]
const thornBushEid = BigInt(thornBushStr)

world.setParent(thornBushEid, schema.mainSystemId)

Alternatively, consider saving the Eid directly in your schema as an ecs.eid type. This way, you won’t need to perform the conversion every time:

schema.thornBushEid = thornBushArray[0]

world.setParent(schema.thornBushEid, schema.mainSystemId)

Thanks for your support!

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.