Quickie: Organising Your Code with #region

I’ll admit, this is a particularly quick topic for an article but I just fancied getting this down in case you’re not already familiar with it.

Aside from using comments to divide up your code for easier reading, we can also use two of what are referred to as ‘preprocessor directives’ to organise our code:

Using ‘#region [RegionName]’ and ‘#endregion’ lets us define the start and end point of blocks of code. These can then be minimised or maximised as and when we wish. I typically have a region for my Start/Setup methods, a region for the Update method (and any other methods which are called during it), and then categorise the rest according to what feels most appropriate. This can make large and potentially overwhelming scripts a lot easier to manage and navigate when you can minimise the ones you aren’t interested in and expand the ones you are interested in.

I typically define a bunch of custom event types (e.g. ‘Vector3Event’), so try to keep these separate from the specific events based off these

I’ve also found this sort of thing is really helpful for just encouraging me to think a little bit more about how I can make my scripts as readable and accessible as possible. I’m sure having single scripts which expand to well beyond 800 or 1000 lines might not always be a sign of good practices (for instance, having one script basically control the entire game), but if a script does indeed get this large then something like this can really help with keeping things manageable 😉

--

--

Game Developer | Game Design and Literature Graduate

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store