x


Script Organisation C#

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!

more ▼

asked May 28 '12 at 08:53 AM

Alismuffin gravatar image

Alismuffin
111 17 24 25

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered May 28 '12 at 11:19 AM

Bunny83 gravatar image

Bunny83
46.8k 12 50 210

Thanks a lot for all that! I have been using regions in monodevelop and they are very nice to use. And organising functions isn't a problem. But one thing I've been wondering about is if it is possible and practical to use multiple update functions in one class

For example I have a virtual pet AI class in which it controls the pets actions through initiating coroutines based upon the pet's mood and status.

When the script Starts, it initiates a void method which tells the script to start a grouped action base upon two enumerations(MoodState1 and moodState2). If the pet is happy and curious then it will start a coroutine called explore. Within that coroutine it activates other coroutines which make up individual actions such as walk in a direction and inspect certain areas.

However for the pet to walk, all movement needs to be under the update function which leaves me with all my script being tidy except for a very long update function which I have split into regions, but still can be a hassle.

May 29 '12 at 12:39 AM Alismuffin

Just call functions from your update function if it helps keep stuff organised.

I used a state machine I wrote that actually calls different Update routines based on the current state - I find this very practical - easy to keep all of the state based functions in one region too.

May 29 '12 at 12:45 AM whydoidoit
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered May 28 '12 at 09:17 AM

whydoidoit gravatar image

whydoidoit
33.6k 14 23 105

And I really miss Extract Class to Own File refactoring from Visual Studio, hopefully it will turn up soon :)

May 28 '12 at 09:25 AM whydoidoit

Thanks for your tips! Very useful :)

May 28 '12 at 09:34 AM Alismuffin

Is there a disadvantage to having a great deal of scripts? I'm recreating my game in a cleaner mode and I'm not sure whether I should do different scripts for each UI screen or not

Eg: SplashScreenUI.Cs ModeSelect.Cs Options.Cs Etc

OR

UI.Cs

In my original script I used enumerations to cycle through menus

May 28 '12 at 09:45 AM Alismuffin

I cannot really see a disadvantage. Maybe compile time is longer, but that's made up for by the improvement in development time.

May 28 '12 at 10:59 AM whydoidoit

Yes, the compiletime can be a little bit longer, but almost neglectable. If you worry about compiling time it's by far more effective to use only one language and don't spread too much into the different compiling groups.

If you have a lot editor scripts that aren't in development (so they are finished) you can compile them manually into an assembly, but keep in mind you can't reference other scripts in this case.

Btw. All your scripts get compiled into one assembly during compilation (one assembly per compiling group), so seperate scripts doesn't care at runtime since they don't exists ;)

May 28 '12 at 11:26 AM Bunny83
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4370
x63
x5

asked: May 28 '12 at 08:53 AM

Seen: 1139 times

Last Updated: May 29 '12 at 12:45 AM