Apologies if a question like this has already been asked, (could not find a suitable answer)
I’m trying to restrusture my single file app.js to a more structured approach, and with the ability to separate all my different screens & functionalities, in this case:
There is no single solution for scene organization, and each project has their own requirements. I don’t see anything very bad with your scene, through you could stop the render loop while the scene wasn’t ready. If you want to get more examples of organizing scenes, I highly recommend @jelster 's book and accompanying repository Available now to pre-order - new book on how-to BabylonJS! - Announcements - Babylon.js
Thanks for the shout-out, @carolhmj and welcome to the community @oriongu!
I think you’re off to a good start with the code you posted. Two things I can think of for you to keep in mind or that might help would be these:
don’t invoke create from your scene class constructor. Instead, your scene management logic in app should call that. Internally, the create call can await scene.whenReady
your render loop appears to reset the scene every frame - as was mentioned, that’s an area that can be tightened up
What can I say. Now we know why You wrote this book . Though, from a marketing perspective, you should have pushed for the purchase of a hardcopy . Not as if @carolhmj didn’t offer it to you Here you’re just giving the answer for free @oriongu Please note the joke only in this post. It’s actually part of BJS culture to help the best we can, fast and for free. Among the things I appreciate (and use ) a lot here. And then, so do I (for whatever the little my advise is worth )
When in doubt, I copy the way the SpacePirates do it (in the BabylonJS repo).
The way your doing it looks good and similar to mine. I have the main render loop in my index.ts and switching scenes is basically just switching which scene is the current one similar to yours.
I also have d.ts for any global needed game logic, maybe this isn’t smart however…
I’ve restructured slight the game loop and scene classes following you guys advice, scene is only rendered once now until we change state.
I’m still getting some “No camera defined” until the player is actually initialized, but it looks like it’s working as I want. I guess I must create the camera in the gamescene and then pass the reference to the player class, so that there is always a camera. even during “loading”.
Additionally, if I try to draw the game gui (this._setupGUI()), the whole things crashes, but I think if I solve the above, I should be fine,
On the fun side, i’ve added nameplate to the players, it looks great . I now have a basic functionning lobby and game movement inplace