Sunday, January 5, 2014

Code Organization (DragCore)


     My code is much more organized this time around. I find that it helps to separate scripts into things as specific as possible. I made one for each game scene (there are five total) and one for each object that moves and animates itself. My old way was to have a giant GameControl script full of commands for different menus and modes. It was controlled my a mode variable stating which scene was active. It was a mess searching through that thing and I kept finding myself putting things in menus where they didn't belong.

     Now I'm using separate scripts, one attached to each game controller object in every scene. This way, the only functions a scene can access are ones that are relative. All without messing up things in other menus and realigning a bunch of curly braces every time I slip up. I also take care to keep my Update() method clean. I used to put hundreds of lines of alignment and setup code in there, making my games sluggish. Now I only setup once by calling a refresh method in the Unity Start() method and make changes when a button is pressed.

     Messy code leads to bugs that I hate with a passion and these practices keep me away from them, just a little more. It seems obvious to me now, but when I started out I never cared what the code looked like as long as the game worked. Now I'm practicing efficiency and dynamic systems which is what code really needs to be in the long run.

No comments:

Post a Comment