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