- Home /
How to be efficient?
This troubles me, when I see other people projects, everything looks like cogwheels connected one to each other in such a perfect way. While I work on my project, I always find myself changing and reinventing my work in a different way so everything will fit.
I'll give you an example:
If I for instance want to create a player, who can shoot. I can perform shooting in two ways, wether to instantiate a prefab, or using raycasting. Each of these options will resolve as a chain reaction in future work. and if I suddenly decide I need a different approach in my game, I need to rewrite everything.
In case I want to create a health manager, should I add it for AI's? or just the players, and then recreate a similiar script with health to my AI.
By the end of the day, I'll have 20 scripts, and I haven't finished my work yet. I feel I'm doing something wrong, and I don't know what to do in order to fix it, maybe it because my lack of knowledge in programming. I know how to write, I'm using UnityScript by the way, it's just that I feel it is too messy.
How can I solve this problem? It's not just a specific problem I need to think of, it's my whole attitude for game developing.
Answer by Kryptos · Sep 10, 2012 at 11:36 AM
To be efficient, consider this statement: divide and conquer. You need to divide your object into smaller part that are self sufficient. You can then reuse these parts more easily.
Unity enforce this rule because objects are component-based. Each component should be responsible for a particular task and nothing more. It is better to start with too much small parts than a big fat script with 1000s of lines.
Consider an enemy bot. This bot can move. It can fire bullets. And it is also a kind of AI. You could use a unique script to do that. But I recommend to make three scripts:
The Locomotion script: its task is to move the enemy bot.
The Gun script: it controls the gun and the bullet that are fired.
The Brain: it contains the AI algorithm. This is the main script that will use the Locomotion and the Gun scripts when needed.
Advantages of this approach: you can reuse the scripts on other objects. You can even switch one script for a more specific one without recoding everything. For example, the big boss enemy bot with use the same Locomotion script, the same Gun script but a different Brain.
Of course, if you have too much small parts, it can become a mess to maintain. And it is not always good for performance. But keep in mind than performance is an issue only when you ship your game. It is a waste of time to try to make the most efficient scripts at the beginning because most of the scripts will be modified or thrown away. So at the end of the development, you can consider merging very small scripts into bigger scripts. And this operation is very simple: just copy paste everything into a new script. On the other hand, splitting a big script is very difficult and error prone.
Back to your examples.
I can perform shooting in two ways, wether to instantiate a prefab, or using raycasting. Each of these options will resolve as a chain reaction in future work. and if I suddenly decide I need a different approach in my game, I need to rewrite everything.
Write a script for each way. Write a base script that contains all common code shared by these two ways. Make each script inherits the base script. In the rest of your project, only use base script reference. Whenever you decide to use a way or the other, you just change the script used without modifying the rest of the code.
In general, try to learn the basic object-oriented design patterns. The bible in this domain is the book from The Gang of Four which title is... (I will let you guess...) Design Patterns.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
need help with a script 1 Answer
How do i make an animation play on key press? 3 Answers
Bullet Fire script not working 1 Answer