Restarting scene on timer

Hi everybody!

I’m trying to make an AI project with evolutional algorithm
I have to do the following:

  1. I start the scene
  2. start NN algorithm, and it works somehow in the scene
  3. collect the results
  4. do something, and start the 1st step

And I need to restart it on timer.

I didn’t find ready-made solution to my problem. I’ve tried the folowing idea:

var scene;

scene_Func = function(){
code, generating the scene
}

some_Func = function(){
scene_Func();
scene.dispose();
scene = null;
}

window.addEventListener(‘DOMContentLoaded’, setInterval(some_Func, 5000) )

This could work fine, but, every next time I restart the scene, everything starts working slower and slower.

If this solution is wrong, could you please help me how to do it properly.

P.S. Sorry for my bad english

if correctly disposing the scene and recreating it right after there shouldn’t be any degrade in speed/performance.

It is hard to say what is done wrong, as we only see pseudo-code and cannot reproduce this.
Please create a playground or show us a working example.

Also - why don’t you leave the scene as it is, but reset its meshes?

scene.meshes.forEach(m=>m.dispose());
generateMeshesForScene();

I’ve created playgroung

https://playground.babylonjs.com/#20D83J

there you go - https://playground.babylonjs.com/#20D83J#4 :slight_smile:

If you have any questions, let me know

In general - the playground should be structured in a specific way in order to work. What I do is create the scene once, but clear the meshes and recreate them every 5 seconds using the interval.

Also notice that I am clearing the interval when the scene is disposed, which is very important

Thank you very much for your answer!
But it seems, that some meshes duplicate


What can be done with it?

clear the meshes array :slight_smile:

https://playground.babylonjs.com/#20D83J#6

1 Like

Thank you very much!
I don’t really understand, how is it working, but I’ll try to get into it :sweat_smile:

the idea is that the entire mesh creation is encapsulated in a single function, and the scene first clears all of its meshes and then creates them.

This is a very naive approach, without understanding the usecase. I guess there could be a better way (probably with disposing only specific meshes) but this is up to you

1 Like