Search Unity

Decoupling Project Dependancy

Discussion in 'Scripting' started by mvinc006, Oct 23, 2018.

  1. mvinc006

    mvinc006

    Joined:
    Jan 1, 2018
    Posts:
    91
    I have a project that has scripts which contain functions relevant to the script title, for example “tile_generation” will house all the functions required to manipulate game tiles (custom unity tiles).

    Recently I’ve been shifting from using Singletons to call the functions, to using UnityEvents and scriptable-objects (as discussed in Unite Austin 2017).

    I’m stuck as to how far to take the module approach.

    Looking at my first paragraph as an example let’s say that script contains 20 functions, which at any time can be called, is it better to instead split each function into its own file? Or keep them grouped based on purpose?

    I should point out that none of the scripts rely on data being passed into it, the scripts reference variables that are scriptableobjects in the scene, for example
    class myFloatVar : scriptableobject 


    Edit: data is passed in via inspector (dependancy injection)
     
    Last edited: Oct 23, 2018
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Passing data in (otherwise known as dependency injection) is a pretty decent tool for code decoupling.
     
  3. mvinc006

    mvinc006

    Joined:
    Jan 1, 2018
    Posts:
    91
    Sorry I think I misrepresented what I am actually doing, which is just that, dependency injection via the inspector (edited the body)

    However splitting a script into 20 smaller scripts isnt something I’m keen on and was after advice on if that’s better if following a true modular approach.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    My "datasacks" repo does this and is a great way to decouple all the Unity UI stuff from your code. No more drag-dropping bits of UI into script instances in scenes and prefabs: just wire up scripts, drop in datasacks, and make your code listen into datasacks or write to them.

    You can also use them for all sorts of intra-program and inter-module communication. They function basically on a lightweight subscriber/publisher/listener-ish model. That model can be as ad-hoc or structured as you want, so it adapts well to Unity's "wide open" publicly-accessible scene hierarchy.

    https://bitbucket.org/kurtdekker/datasacks

    and

    https://github.com/kurtdekker/datasacks

    20180724_datasacks_overview.png
     
  5. mvinc006

    mvinc006

    Joined:
    Jan 1, 2018
    Posts:
    91
    Thanks I’ll keep that in mind.
     
  6. mvinc006

    mvinc006

    Joined:
    Jan 1, 2018
    Posts:
    91
    So to bump this one last time, as my question hasn't been addressed yet (thank you to Kurt-Dekker and Kiwasi for their suggestions thus far)

    With scripts that have a set of functions by purpose (see my original example above) are they best left alone or split into their own individual file to help with keeping things more modular?

    (why does this matter? well its just how I seen them do it on the Unite Presentation, kept each function on its own)
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Yes. :p

    Honestly it doesn't really matter that much. The general guiding principle is single responsibility. If all of the functions are related to the same task, leave them together. If they all do different things, split them up.
     
    mvinc006 likes this.
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    This is a great second guiding principle.

    I prefer "the game must ship" as a first guiding principle though. :)
     
    mvinc006, neoshaman and Kiwasi like this.
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I can't disagree there.

    Some of the last minute hacks I've pulled off to met a deadline are atrocious.
     
    mvinc006 and neoshaman like this.
  10. mvinc006

    mvinc006

    Joined:
    Jan 1, 2018
    Posts:
    91
    The downside for me is I’m my own worst enemy, I don’t have to answer to anyone but myself as a solo developer. It leads to a lot of procrastination, not working to rule.