Search Unity

Please help me improve my coding, inheritance, and accessing scripts and objects

Discussion in 'Scripting' started by dangermouse, Nov 29, 2012.

  1. dangermouse

    dangermouse

    Joined:
    Nov 28, 2009
    Posts:
    73
    Some very brief background. I've been coding for a long time in multiple languages, but almost always on a single class, or solving a single complex problem, and having to do it very quickly. So in that vein, I've gotten used to a lot of sloppy coding, including global variables, singletons, and copying and pasting entire classes. In fact, often things are structured that I don't use OOP at all.

    I've spent quite a bit of time looking through the forums and unity answers, but with so many thousands of posts, I'm having trouble finding what I'm looking for, and instead I'm just reading a whole lots of opinions on why things shouldn't be done, instead of the best approach to get it done.

    Now is time for a new start though, I'm the tech lead on a small indie team. But I know for a fact my coding practices will not fly here. We're going to be doing a fairly straightforward RPG, with a main hero, an inventory, and lots of enemies and NPC's.

    I'm not asking for any concrete answers or examples, but rather some direction on two topics.
    1) What's a good way to organise my classes and sub classes. Should I use a lot of inheritance? Should I use a master-class? Should I use classes for each type of thing? etc. As I've said, I'm only used to dealing with one or two classes or objects at a time.
    2) What's a good way to access other classes and scripts? Events? Dragged references? GetComponent? I'm guessing not singletons, and global variables / references ;)

    So If you have any insight, or preferably a link to a good resource / book / tutorial, I'd be really grateful. I really want to be the best I can be for my team, and not waste too much time. Incidentally, it'll all be C#, simply because I'm already familiar with it, and on the surface it seems the cleanest and most flexible.

    *** About me, if you're wondering. I work at Weta Digital, and have just come off the post production end of The Hobbit. I work in special FX, so most of my coding is in the form of particle expressions, iterating through huge array's of plants, that kind of thing, and usually in Python or C++. A far cry from organising the objects in a game! Also, because I'm just coming off of a film, I'm very tired from working hundred hour weeks, so if my post is ridiculous, annoying, or has been answered a hundred times already, at least yell at me creatively :)
     
  2. tomvds

    tomvds

    Joined:
    Oct 10, 2008
    Posts:
    1,028
    My advice is always: try to use composition before inheritance. Unity is based around object composition and a composition based solution usually leads to better design compared to inheritance. I'd highly recommend googling for the "composite design pattern" and "composition vs. inheritance". It will give you plenty of literature on this topic.

    Dragged references work fine within prefabs. As soon as you have references pointing outside of a prefab, porting these prefabs to different scenes becomes a mess as soon as your project starts growing in size. Therefore, I recommend to never have dragged reference pointing outside of a prefab ever. Our current approach to references outside of a prefab is to find the game object(s) by tag and then GetComponent(s) to get specific components. It is a bit more work to set-up (especially with the clumsy interface Unity has for adding and assigning tags :S), but it saves you a lot of time in the long run when scripts are able to set up communications automatically.
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    I've got a bit to say on the subject, I may come back around to say it. A little busy right now. For now though...

    I would like to emphasize tomvds's first statement though. The preference of composition over inheritance is a highly subjective topic, and neither is always the right answer. It's in the case of Unity that composition tends to be the better choice, because that's the choice the unity developers took by using the 'Component' model (which is an extension of the composite design pattern). It's best to use in unity, because unity already chose that for you.

    Another thing you'll want is to read about design patterns.
    http://sourcemaking.com/design_patterns

    Here is the sub-link at that site for composite pattern:
    http://sourcemaking.com/design_patterns/composite

    As usual though with anything programming. The use of design patterns is also a highly subjective and debated thing. Some people believe that they too are imperfect and serve only to highlight that OOP is actually broken. They are useful things, just keep in mind design patterns aren't dogma.

    Back on the topic of unity, your components/monobehaviours are what implement the component pattern. You don't always have to use them... the unity documentation can be confusing and implies that you must inherit from them. You don't.

    This is when choosing between inheritance and composition comes into play. If you're not inheriting from monobehaviour, the choice of composition over inheritance starts to blur. And you may start to use inheritance if it better suits the task you're attempting to accomplish.




    Anyways I'll be back.

    You have any more specific questions? OOP is a fairly large topic.
     
  4. kablammyman

    kablammyman

    Joined:
    Nov 22, 2010
    Posts:
    507
    agreed.

    OP, you dont want to fight unity by going against its component model, but at the same time, there are times when inheritance is better suited for certain tasks. A lil pre-planning and foresight can go a long way when you are coding.
     
  5. dangermouse

    dangermouse

    Joined:
    Nov 28, 2009
    Posts:
    73
    Thanks so much for the direction guys! It actually warms my heart that you took the time to write detailed useful replies for me. I'll be going over all of this, as well as the google searches and links, in great depth.

    @tomvds: That makes a lot of sense, thanks! I'll be googling those terms this afternoon and aiming to reach a better understanding. I'm especially glad for your brief explanation of using dragged references within prefabs, and not externally. That makes good sense.

    @lordofduct: You raise a lot of good points, especially that no one model is perfect. From where I'm standing though, they're a damn good place to start, and thank you for the links. At the moment I'll focus on the composite pattern, but being aware of everything on offer is always wise. I look forward to heaing more if you find the time!

    @kablammyman: Definitely, at this point I want to get learning, and get my hands dirty playing with this stuff. I don't want to be fighting against what's already there. But yes, there will be a lot of research and planning going into this.

    Thanks everyone, and keep it coming, these are exciting times!!