Is there an ideal, better way to organise scripts?
Is it recommended to create many name spaces, or not really?
I’m wondering because the way I’m scripting seems very untidy. I hav to search within each script for a while to find certain sections of code. I have lessened a lot of code by writing dynamic methods with parameters so they can be recycled over and over. But I really want any tips anyone has on keeping scripts nicely organised.
Is it better to attach individual scripts to many objects, or better to save them as variables in one script?
Sorry Im full of questions like this. I just want to know an ideal and efficient way of creating scripts >.<
Thanks!
To add to @whydoidoit answer:
- Seperating scripts is important, but don’t split every function in a new script. Group together what fits together.
- If a function gets too big / confusing, split it up into multiple functions where it’s possible or use regions( see below ).
- If a class / script gets too big / confusing and it can’t be reasonable split into seperate files, you can use
#region
statements to group code together.
Example for #region
public class MyCharacter : MonoBehaviour
{
#region Unity callbacks
void Start()
{
/* ... */
}
void Update()
{
/* ... */
}
void OnTriggerEnter()
{
/* ... */
}
// ...
#endregion
#region Movement
void HandleMovement()
{
//...
}
#endregion
#region Inventory
//...
#endregion
}
Regions can be used every where in the script. They are completely ignored by the compiler but in good editors (I use only Visual Studio but MonoDevelop should support this as well) you can collapse the whole region and it just displays the text behind the region statement.
Also keep in mind that you can also collapse any function or class body. Regions can also be used inside a function to seperate parts of code.
void Start()
{
//Some code here
#region Init variables
//Long list of variable initializations
#endregion
}
When using regions, don’t overuse them, this will make a search even more complicated. Use exact and descriptive region names. If you search for a piece of code and you don’t know in which region you have to look, the names are bad choosen. Also you should check the color highlighting of regions / preprocessor tags. Use a very signalling color (I use plain cyan) so a collapsed region doesn’t get lost between the lines.
Just in case you can’t find where you can collapse a region / function / class, there is a “-” sign infront of the code line. When you collapse a section it turns into a “+” sign
A final note: endregion tags can also have additional text behind it. It isn’t used somewhere, but for very long regions (or nested regions) it helps to have the same text on both the region and the endregion tag.
Ok, first of all if you are using MonoDevelop it has a search for symbol keyboard shortcut, CMD + . on Mac that lets you start to type a class name, a variable name, anything really and then it auto completes, fantastic way of navigating your solution if you can remember any part of the thing you want to go to. Go to definition when the cursor is over something and return from that with (Mac) CMD+D and CMD+SHIFT+D
Its down to style, but I use this method:
- each class gets its own file. That way you can easily CTRL-TAB between source, not waste half a day scrolling.
but I embed utility classes in the outer one, effectively using it as a namespace.
-
the file goes in a folder which is in a tree. Mostly to make it easy to find if I forget all of the names in it, can’t find some code to refer to it and hence have to resort to looking in the solution window. Therefore my folder names are usually “concepts”. Like state machines, experts etc. I code in MonoDevelop with the solution window hidden, that’s how good Navigate To is.
-
I use namespaces for sub systems that are nothing to do with Unity
If a class is too long, you can split the class into partial classes in different files.