Jump to content
  • Advertisement
Sign in to follow this  
jmeyer2k

Well Structured Code

This topic is 2096 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I've been game developing for around 3 years now and most of my games, I give up on since they seem hard to code because of a lack of structure. I've always wondered how to write games in a way that it is easy to read and object oriented so that I can easily get and set a players health. Also, how would I do this in javascript since it is a little bit different than most programming languages.

 

I will keep updating this post with useful info:

  1. http://androidnirvana.comule.com/wordpress/2011/11/structuring-a-game-using-object-oriented-design-patterns/
Edited by jmeyer2k

Share this post


Link to post
Share on other sites
Advertisement

I've always wondered how to write games in a way that it is easy to read and object oriented so that I can easily get and set a players health. [...]

So innocent smile.png

Share this post


Link to post
Share on other sites

JavaScript and Object Oriented is a strange beast.  JavaScript isn't actually an object (class) based language, although that doesn't stop people from using it like one.

 

JavaScript instead is a Prototype based language.  In a nutshell, this means in JavaScript you describe something ( say... an NPC ), then clone and possibly extend it.  "Objects" in JavaScript are mutable to the extreme.  This is handy in that creating adhoc objects is laughably simple, but dangerous in that... well, clobbering an existing object is laughably simple.

 

JavaScript have some major shortcomings though.  It's far too easy to create globals ( it's the default... ) and globals are bad, m'kay.  It's also far too difficult to hide data.  There is no concept of private or protected variables for example.  This makes using JavaScript on large scale multi developer projects... tricky. As does the lack of a good module system built in.  There are solutions out there to try and solve these problems...  from closures ( which are an overly complicated hack to accomplish something that should be simple ), to higher level languages like CoffeeScript and TypeScript that compile to JavaScript.  Of course, there is also the next version of JavaScript that hopes to fix all of these problems but is a product for the future.

 

 

So frankly, JavaScript is great at creating in the small projects.  But for larger solutions, you really want to use a framework.  Building your game over an existing library like Backbone would impose structure and a certain about of cleanness on your code.  Or use TypeScript or a similar higher level language.  One of the nicest things about JavaScript is also it's mutability, so its almost staggering what a framework is able to accomplish when it comes extending JavaScript or hiding it's various flaws.

 

 

Also, pick up the book JavaScript:The Good Parts.

Share this post


Link to post
Share on other sites


It's also far too difficult to hide data.  There is no concept of private or protected variables for example.

 

You can still annotate a block of declarations as public, and treat everything else as private.

 

People typically settle on some convention.

Share this post


Link to post
Share on other sites

Serapath: Thanks, I think I will try using normal javascript and just try to be as clean of a coder as I can :)

 

Bacterius: Also thanks, I will look in this and try to implement it for my game.

 

Thanks everyone!

Share this post


Link to post
Share on other sites

I faced this problem myself and the best thing I done was learning how to use UML and (way harder) learning to always use UML; I usually was too lazy to model everything beforehand and ended up in those "what do I code now? what do I need now? how do I do this?" obstacles.

 

Today, I use a software called ArgoUML for my modeling and then, and only then, I start to code. The software can even generate code structure for us.

Edited by dejaime

Share this post


Link to post
Share on other sites

As I'm learning JavaScript myself, I cannot avoid noting how JS documentation is really dependent on data types being there.

This separation between data types as something that just exists in our mind to understand against data types as something understood by the language is JS main problem in my opinion. Code readability, as well documentation is seriously hampered.

So sadly, in the long run, I consider static, traditional languages to be far superior.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!