Jump to content

ian
 Share

Recommended Posts

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 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

@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 :)

Link to comment
Share on other sites

Just joining in - when you get down to it, most games are structured like:

  1. At startup: Initialize all state variables
  2. Each tick: process user inputs and update state variables
  3. 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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...