Hi
I managed to re-create the issue where the project reload itself in an infinite loop, and seems to be caused by switching camera from a world camera to a 3d camera
Whatβs happening here is:
1 - At the state BattleArena
and WorldExploration
are hidden from the studio
2 - GameManager has a state machine and dispatches a switchScene
event at the start
...
stateMachine: ({world, eid, schemaAttribute}) => {
ecs.defineState('starting')
.onEnter(() => {
world.events.dispatch(world.events.globalId, 'switchScene', {scene: 'battleArena'})
})
.initial()
},
...
3 - SceneManager listen to switchScene
, hide both BattleArena
and WorldExploration
, then display BattleArena
(remove the hidden component) and switches the active camera to the relevant one:
...
function switchScene(e) {
console.log('switchScene', e.data.scene)
if (e.data.scene === 'battleArena') {
hideAllScenes()
ecs.Hidden.remove(world, schemaAttribute.get(eid).battleArenaScene)
// if I remove camera set active it seems to behave as expected, but unfortunately I need
// to swtich as I am switching from a world camera to a 3d camera
world.camera.setActiveEid(schemaAttribute.get(eid).battleArenaCamera)
}
}
...
Is there anything that I am doing wrong?
Thanks a lot
Full code and videos below
Game Manager
import * as ecs from '@8thwall/ecs' // This is how you access the ecs library.
ecs.registerComponent({
name: 'gameManager',
schema: {
// Add data that can be configured on the component.
},
schemaDefaults: {
// Add defaults for the schema fields.
},
data: {
// Add data that cannot be configured outside of the component.
},
stateMachine: ({world, eid, schemaAttribute}) => {
ecs.defineState('starting')
.onEnter(() => {
world.events.dispatch(world.events.globalId, 'switchScene', {scene: 'batteArena'})
})
.initial()
},
})
Scene Manager
// This is a component file. You can use this file to define a custom component for your project.
// This component will appear as a custom component in the editor.
import * as ecs from '@8thwall/ecs' // This is how you access the ecs library.
ecs.registerComponent({
name: 'SceneManager',
schema: {
// Add data that can be configured on the component.
battleArenaScene: ecs.eid,
battleArenaCamera: ecs.eid,
worldExplorationScene: ecs.eid,
worldExplorationamera: ecs.eid,
},
schemaDefaults: {
// Add defaults for the schema fields.
},
data: {
displayedScene: ecs.eid,
},
add: (world, component) => {
// Runs when the component is added to the world.
},
tick: (world, component) => {
// Runs every frame.
},
remove: (world, component) => {
// Runs when the component is removed from the world.
},
stateMachine: ({world, eid, schemaAttribute}) => {
function hideAllScenes() {
ecs.Hidden.set(world, schemaAttribute.get(eid).worldExplorationScene, {})
ecs.Hidden.set(world, schemaAttribute.get(eid).battleArenaScene, {})
}
function switchScene(e) {
console.log('switchScene', e.data.scene)
if (e.data.scene === 'battleArena') {
hideAllScenes()
ecs.Hidden.remove(world, schemaAttribute.get(eid).battleArenaScene)
world.camera.setActiveEid(schemaAttribute.get(eid).battleArenaCamera)
}
}
ecs.defineState('starting').initial()
.onEnter(() => {
world.events.addListener(world.events.globalId, 'switchScene', switchScene)
})
.onExit(() => {
world.events.removeListener(world.events.globalId, 'switchScene', switchScene)
})
},
})