- Home /
How do you organize a character control script to avoid spaghetti code?
I just started on a player control script, and with less than half of the functionality I want the character to have, it's already an incomprehensible mess. (it unfortunately resembles the desktop on my computer and the actual top of my desk.) I was adding one thing at a time and now have a ton of variables and my Update() is pages of if / else if /else statements with multiple &&s and ||s in the ifs. So now I'm having issues where it starts spazzing out and getting stuck in loops of jumping and it's hard to track down why.
I need to start over and rewrite it with care, but not sure how to do things differently so I don't get back to the same problems. Do I split up everything into little functions, and only call functions from Update? Should I have several scripts attached to the player; one to just handle walking, one for jumping, one for climbing, one for throwing things, etc?
I know this is more a general programming problem than a Unity problem, but one that probably everyone who is self-taught in scripting and first takes on more than an extremely simple game is faced with.
How did you solve it, and/or what should I go read?
Answer by Yokimato · Nov 06, 2012 at 04:36 PM
Think modular!
What I have is an Update method that will call a number of simple, small, and most importantly easy to ready methods. One to gather input, one to handle moving, one to handle actions, etc. Sometimes you're just not going to get away from a lengthy method (Ones with switch statements, for example), and it's important that you don't break it up for length alone...however it's good to refactor your code often to ask yourself "Does this belong here?" and "Does this action deserve it's own method?". More often then not, you can keep your code clean by simply going back and re-reading what you just wrote. If you google some refactoring methods, you could get some more indepth ideas, but overall, my vote is to think small, think modular, and be your own code review in lieu of others. Good luck!
Answer by MibZ · Nov 06, 2012 at 04:43 PM
When I was first being taught how to program, the only thing we were allowed to have inside the Update function were function calls because that makes it MUCH easier to debug your program.
That said, when I was first learning how to program the most complicated game I programmed was Tic-Tac-Toe. (More complicated than you would think, though!)
I wouldn't say that you need to only have function calls in Update, but I would recommend making good use of regions - they can be VERY helpful for organizing your code into readable blocks, but you need to be as careful setting up regions as you are setting up parenthesis ()! You need one #endregion for each #region or else you will get errors.
#region Region summary goes here
//TODO: Insert code here!
#endregion
When regions are in your code, there will be a box with a plus or minus in it next to the region that lets you expand and collapse it. Regions can be as long or short as you want, you can put a region around one line of code or from the first line to the last line (that would be silly though!)
Another important factor to organization is that you break your functions down into easily manageable sections, three functions that are 50 lines each are easier to read, write, and fix than one 150 line function.
Keep at it, once you start organizing things a particular way keep doing it, after a while that method of organization will just be how you write code.
The last tip I'll leave you with is that you shouldn't always be trying to reorganize things, but you should regularly stop coding new things to look over what you have so far and make sure that it is doing both what you want it to do and that you don't have useless or redundant code.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Enemy not moving towards Player 0 Answers
Square Character Controller? 4 Answers
How do I force a GameObject with children to collapse and expand in the hierarchy view? 1 Answer
Best way to go about believable 2 Character Grab/Lift animation? 1 Answer