ian Posted October 21, 2016 Share Posted October 21, 2016 Hi BabylonJS users explorers developers. Is there any example code how to organize game code? Is there any example how to organize different type script classes and connect them together? Is there any sample how to divide code into classes? Any blueprint or good practices? Unity has Game objects. How can we create Our game objects with Babylon? How should we organize classes for example class for render class for controls attaching separate scripts to game objects.....? Any advice or example? Actually I would like to see any example for good game code architecture in typescript. Greetings Ian Quote Link to comment Share on other sites More sharing options...
Wingnut Posted October 22, 2016 Share Posted October 22, 2016 Hiya Ian... good to see you again. Sam Girardin made a powerful demo using Babylon.js, and he gave us a great source code package. In the demo, press x30, then press F button repeated... timed-to the beat of your favorite song. Watch the old men dance to the music. FUNNY! Jump them TOO high, and they might get stuck in floor! Oh no! heh I think it uses an older version of BJS, included in that source code zip. It is a typescript app... and Sam is an excellent coder. I believe the entire Visual Studio "project" is included in that zip file, I think. And his ragdoll app... although not really a game... is still VERY fun, and it works great. For general great game structure (non-typescript)... Temechon does it right - check his website. Lots of nice game demos, and most of them "OOP"-ish. His e-book is rumored to be quite good, too. I like the way Temechon writes games. He always keeps code re-use in mind, and knows how/where to separate his classes. Names them well, too. Hope this helps. gryff 1 Quote Link to comment Share on other sites More sharing options...
max123 Posted October 22, 2016 Share Posted October 22, 2016 @ian, this is a great source for Typescript; pretty much everything you need to know about it, including set up and how to combine classes. Ultimately, it all boils down to how you want your exported code to look like (es5? es6? single file? modules?). There's no single opinion on code structure Quote Link to comment Share on other sites More sharing options...
max123 Posted October 22, 2016 Share Posted October 22, 2016 If you have any experience with strongly typed languages, you'll find Typescript super easy. Took me about a day to hit the ground running. NasimiAsl 1 Quote Link to comment Share on other sites More sharing options...
adam Posted October 22, 2016 Share Posted October 22, 2016 If you like the way Unity does it, look up "entity component system typecript". Quote Link to comment Share on other sites More sharing options...
fenomas Posted October 22, 2016 Share Posted October 22, 2016 Just joining in - when you get down to it, most games are structured like: At startup: Initialize all state variables Each tick: process user inputs and update state variables Each render: update 3D engine properties from the game state Ticks and renders can happen simultaneously or at separate speeds. Apart from that it's all a matter of getting inputs and manipulating game state each frame. As for structure, personally I'm not a fan of heavy OOP for games - I find Entity Component Systems much more flexible. I couldn't find one that quite met my needs though, so I wrote one: ent-comp. It's in plain JS, not TS, but just in case anyone finds it useful ian and jerome 2 Quote Link to comment Share on other sites More sharing options...
max123 Posted October 22, 2016 Share Posted October 22, 2016 My personal observations: - myClassInstance.myPublicVar.property is always faster than myClass.setMyPublicVarValue(value:number) {this._myPrivateVar = value}- direct property manipulation is faster than method invocation. The difference is trivial in most cases, but not in games where you might be making thousands of calls. Horrible practice though if you ask code nazis - separate render and calculation loops and use delta time. Here's a great article explaining the principle. I'm sure Babylon does that for you - a game is almost always NOT your average application and it's worth bending the rules when it comes to code structure/OOP/MVC/MVV/Actor and all other patterns if you value performance over maintainability (I know I do) - if a js code is over 1000 lines, I use Typescript. It helps my sanity. - Use object pools ian 1 Quote Link to comment Share on other sites More sharing options...
adam Posted October 22, 2016 Share Posted October 22, 2016 As fenomas often reminds us, don't optimize without profiling. Quote Link to comment Share on other sites More sharing options...
ian Posted October 22, 2016 Author Share Posted October 22, 2016 it's not about profiling. but about code architecture (code orderliness) (modules and patterns) This is what I would like to see some example. (profiling is about performances and using (and/or leaks of) memory.. If I understand correctly.) Quote Link to comment Share on other sites More sharing options...
ian Posted October 22, 2016 Author Share Posted October 22, 2016 How to organize and load models-komponents from blender/export .babylon into game object models... Quote Link to comment Share on other sites More sharing options...
ian Posted October 22, 2016 Author Share Posted October 22, 2016 thanx to all (I'll take a look around and your hints) Quote Link to comment Share on other sites More sharing options...
fenomas Posted October 23, 2016 Share Posted October 23, 2016 8 hours ago, max123 said: - myClassInstance.myPublicVar.property is always faster than myClass.setMyPublicVarValue(value:number) {this._myPrivateVar = value}- direct property manipulation is faster than method invocation. Note that in general this isn't true. JS engines do tons of optimization as they compile, and depending on how that happens, either version might be faster or slower. In the simplest case, the setter function in your example will just get inlined, and those two code snippets will wind up being identical after optimization. And thanks for the shout-out @adam! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.