Search Unity

Advanced C#

Discussion in 'Scripting' started by alterus, Dec 12, 2015.

  1. alterus

    alterus

    Joined:
    Sep 9, 2012
    Posts:
    59
    Hello
    I read a book on Unity C# programming : Inheritance,Interface,Delegates ,Event Systems and all that stuff

    Definitly their code is more elegant than mine but I wonder whether it is really worthwhile using such sophisticated programming style even for casual games ?
    For example, do the "made with unity" games , published here in the forum, make use of such techniques
     
  2. Sose

    Sose

    Joined:
    Dec 10, 2015
    Posts:
    27
    Software architecture is a subject that you can argue endlessly about. Some design patterns used in the general programming world can be useful for game design aswell. Although Unity already provides a pretty flexible component based system for arranging your game objects.

    How much "architectural design" or "elegance" you want for your C# scripts depends on the scale of the project. Don't do something just for the sake of doing it, unless it's for an exercise. Do what works and if your project is getting too "ugly" or difficult to maintain, consider refactoring the problematic parts of it.

    I think http://gameprogrammingpatterns.com/contents.html is an easy to read book and sheds some light on different OO patterns applied to game design. But when using Unity, it's good to keep in mind that Unity already gives you lots of things like "Update method", Components etc that are introduced in the book.

    The introductory chapter also talks about how much design you need. There's a good middle point somewhere, where your code isn't spaghetti but where you don't make things too general, too elegant just for the sake of doing it. It mentions this "mantra" or principle https://en.wikipedia.org/wiki/You_aren't_gonna_need_it

    For simple games, it's probably better to just do what needs to be done and avoid unnecessary elegance. Actual code design can matter more for projects with multiple programmers and big code bases.

    Edit: though I must add, that understanding key programming concepts such as inheritance, interfaces and delegates is very helpful for being able to come up with solutions to different problems.

    Some time ago I discovered Javascript and thought it was the greatest invention on earth because you could pass functions as parameters and store them in variables, it completely opened my eyes. Something that is doable in most languages anyways, but it hadn't occured to me before. Or the benefits of it
     
    Last edited: Dec 12, 2015
    aer0ace likes this.
  3. alterus

    alterus

    Joined:
    Sep 9, 2012
    Posts:
    59
    It is what I thought
    Unity actually supply a, so to speak, Game oriented architecture
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    Yes.

    See, writing code that follows good patterns is not harder (necessarily) than writing... other code. It's more of a knowledge and habit thing. When you know how to use eg. interfaces, not only does that make your code more robust and readable, it's also easier to write. So it's a win-win.

    But, hey, it takes more practice than just throwing together what you know. Don't feel like you have to master every (or even any) of the advanced techniques to be able to create games. You'll get there eventually, and making stuff you want to make on the way is the best way to learn.
     
  5. phaem

    phaem

    Joined:
    Jan 5, 2015
    Posts:
    76
    If you spent enough time on designing your game architecture, later you'll find out some %% of your code may be used in your next projects without any change or with small modifications, returning the %% time you spent on it in every next game you make. Hiring coders and working with them will be much easier when your code is elegant and shares in the same style and reasoning among many projects. So it definitely worth to learn the art of creating automated systems, not just the language constructs and framework classes.
     
  6. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    using Inheritance, Interfaces, Delegates, Events inst exactly more sophisticated they are just new tools and patterns to solve problems. They often lead to less sophisticated code, or at-least code with a cleaner flow.

    When it comes to learning anything new, it comes down to understanding what problems these systems were designed to solve and also internalizing how this systems work.

    Even experienced programmers are learning all the time. I have been at it for 5 years now and know 6 languages, and when ever i have to look at my old codebases. There are hundreds of things i would have done differently now that i have more experience and think about problems differently.

    Also remember know about new tools dosnt mean you have to use them its up to you to decide how you want solve problems.
     
  7. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,926
    I could not imagine making a game without all these things. Especially Inheritance and Interfaces, but these are not 'advanced' topics, so don't feel intimidated by them. It is possible to make a game without them, yes, but the code will certainly be ugly, "hacky", fragile, and its architecture will be non-existant. Having a solid architecture is crucial when making anything more complex than a game jam game, in my opinion.

    I'm trying to think about how I would go about making a MOBA-ish skills/abilities system without inheritance or interfaces, and it's already giving me nightmatres!

    To answer your question, I'm pretty sure 95% of the games that have made it to the point of being released or featured on madewithunity use most if not all of these things. They let you handle certain situations in a much cleaner, scalable, and fail-proof way. Learning them is essential for any programmer
     
    Last edited: Dec 12, 2015