Using Interfaces in Unity Effectively | Unity Clean Code

Поділитися
Вставка
  • Опубліковано 24 лют 2022
  • Learn how using C# interfaces can help keep your unity code clean and allow you to add new mechanics to your game even faster
    SUBSCRIBE
    Subscribe for more game dev tips and tutorials: bit.ly/3edJUA5
    CODE
    Grenade: pastebin.com/WcZ2ug6d
    ExplosiveBarrel: pastebin.com/jLi30XJ4
    DestructibleCover: pastebin.com/6g3AFJzZ
    EnemyTarget: pastebin.com/7m50eC8p
    FirstPersonController: pastebin.com/ieR6uD5a
    IThrowable: pastebin.com/Z7uLN8n0
    IDamageable: pastebin.com/0RH7jXLM

КОМЕНТАРІ • 112

  • @Kitsomo
    @Kitsomo 2 роки тому +70

    This was a PERFECT explanation of interfaces and their use cases. These concepts seperate hobbyists from professionals. Your way of tutoring is great. You could do a tutorial on object pooling, and how it is used to avoid Instantiating and Destroying objects!! ( I will always recommend new things for you to teach, since I think more people can understand you on these complex concepts) Great work again!

  • @brettabraham
    @brettabraham 2 роки тому +39

    Simple, straight to the point, but chock FULL of good information. Interfaces was one of those things I "planned to learn one day" but now, I feel so much more confident. Great video!

  • @Keanine
    @Keanine 2 роки тому +12

    I already knew what an interface could do, but I wasn't totally sure of the use case for it compared to a base class or an abstract class so I'm glad you added that last part that cleared it up!

  • @GlitchyMF
    @GlitchyMF 2 роки тому +2

    Ok... I spent like 30 minutes trying to understand it but finally.... I get it, instead of checking if its an enemy target and if it is then hitting this enemy target, then if destructible cover then breaking it, then explosive barrel then exploding it.... It just checks if its something damageable then does damage to it, and specifying what the damage does in the objects script itself....
    That my friend..... Is GENIUS... I was always afraid of coding because... Deep in my heart... I knew it was hard and needed alot of memorising (which isn't good for a student) but now i realised.... It just needs understanding

  • @monkeyrobotsinc.9875
    @monkeyrobotsinc.9875 Рік тому +9

    this is by far the best tutorial on Interfaces I've ever seen. ive watched dozens of them, and none of them break it down so simply and especially show a before and after example. really helpful to see. thank you very much.

    • @JamesMakesGamesYT
      @JamesMakesGamesYT  Рік тому +2

      Thanks for the kind words, I might try to do some more content on interfaces in the future

  • @saseb1060
    @saseb1060 2 роки тому +2

    finally a video that has an easy and direct example and not something abstract

  • @thedude110
    @thedude110 2 роки тому +2

    Cheers, mate! I really enjoy how you take fairly complex concepts and explain them in a very simple way

  • @jean-michel.houbre
    @jean-michel.houbre 2 роки тому +1

    Very clear and very well illustrated. I'm lucky to have subscribed!

  • @RetroVemVideos
    @RetroVemVideos Рік тому +8

    Very well put together, you respect your viewer's time and explain plain and simple the concept and give a good example, no BS. Great job!

  • @Stromhammar
    @Stromhammar 2 роки тому

    Great explanation! I had worked with interfaces in the past without even realizing it, and now It all makes sense

  • @Hideyuki_Atago
    @Hideyuki_Atago Рік тому

    Learned quite a bit just from about 4 mins, can already see which parts to use interfaces for which is great, thanks man

  • @davdav4804
    @davdav4804 Рік тому

    Your channel is so good and underrated, thanks you, this was the best explanation I found on interfaces and it helps me a lot to learn game dev !

  • @scull3208
    @scull3208 7 місяців тому

    I can't express how much this 4 minute video has helped me. Thanks so much!

  • @junaidywijaya
    @junaidywijaya 2 роки тому +2

    This is by far the easiest to understand tutorial about interfaces i found online, thank you so much..

  • @foreducation408
    @foreducation408 11 місяців тому

    one of the best tutorial I have ever seen on interfaces in unity.

  • @johnnyfatsacks5209
    @johnnyfatsacks5209 6 місяців тому

    I never fully understood interfaces and this perfectly explains it. Thanks.

  • @aurabeth1680
    @aurabeth1680 11 місяців тому

    Very helpful and gave the WHY and HOW in a very digestible way. Thank you!

  • @williamwan188
    @williamwan188 11 місяців тому

    Super short and clean tutorial. Thanks very much.

  • @flofdev
    @flofdev Рік тому

    Wow this really opened my eye to so much more possebillities. Thanks

  • @vaibhavjaiswal799
    @vaibhavjaiswal799 2 роки тому

    Suddenly interfaces seems easy, really good explanation

  • @tomasroch4905
    @tomasroch4905 2 роки тому +2

    Personally I consider your videos the best of unity learning videos. I absolutely love them for how short and understandable they are, yet they are filled with valuable information. I wish you the best luck in making videos like that

    • @JamesMakesGamesYT
      @JamesMakesGamesYT  2 роки тому +1

      Thanks for the kind words! I've taken a bit of a break from UA-cam but I'm working on more videos for the future!

  • @IlaiyaBharathi
    @IlaiyaBharathi 2 роки тому +1

    Thank you!!!!!!!!! You are a blessing for the game dev community!!! I finally understood interface and you made it look so easy….. thank you thank you

  • @salarmanan323
    @salarmanan323 5 місяців тому

    damn such a great explanaition. definetely deserves a sub. thank you so much man :D

  • @linksys8654
    @linksys8654 Рік тому

    been programing for almost 10 yrs and just know understand what interface does. thanks

  • @thedrumminggamer1064
    @thedrumminggamer1064 7 місяців тому

    this was an amazing explanation, i already used and fully understood interfaces, but this helps increase the Modularity of code , which decreases time to add new features, and removes dependency within code , which can then further be boosted by the observer method

  • @darkmattergamesofficial
    @darkmattergamesofficial 6 місяців тому

    This is incredibly helpful. Thank you!

  • @lcb5651
    @lcb5651 Рік тому +1

    I've finally found a place that explain so clearly a very fundamental use case, I never fully understood this until I found this video, thank you very much for this.

    • @JamesMakesGamesYT
      @JamesMakesGamesYT  Рік тому

      Thank you for the kind words, I’m glad the video helped!

  • @okee
    @okee 2 роки тому

    Short, quick, and helpful

  • @dontthinktoomuch7152
    @dontthinktoomuch7152 9 місяців тому

    The best tutorials i've ever seen

  • @SelaMalka
    @SelaMalka Рік тому

    Amazing video, thank you so much!
    Subscribed!

  • @overyonderstudios9779
    @overyonderstudios9779 2 роки тому +2

    This channel is great, subscribed. You've hit a great niche with videos for people who are finding themselves stuck and need a fresh approach.
    Would love to see a video about structuring projects. One thing that I get stuck on all the time is feeling like I need to refactor my codebase because I didn't architect it well from the beginning.
    Even if I've used the right design pattern for something (if I really believe that a singleton is the right way to handle some functionality) it bugs me when I end up with all my scritps lumped together inside a generic "scripts" folder.
    Should I structure my project based on pattern? (e.g. singleton folder, observer folder, interfaces folder, etc.)
    Or subject? (e.g. items folder, characters folder, enemies folder, puzzles folder)
    Or type? (e.g. prefabs, materials, textures, animations)
    Hard to decide!

  • @jarrettonions3392
    @jarrettonions3392 7 місяців тому

    Thank you. Implemented... wow.. so much easier than the jumble i was working with. Great, consise, easy to follow tuturial.. and exaclty what i wanted and how i wanted it to work. Damage ammount on the thing that actually does the damage... then health on the thing that actually looses health... so simple.. no of these weird incorrectly placed and refenenced tangles

  • @LuzuVlogsGamer
    @LuzuVlogsGamer 9 місяців тому

    This was soo well made I would of believed you if you said that you worked for Unity :O

  • @dreamisover9813
    @dreamisover9813 Рік тому

    Short, and explains the problem as well as the solution very well!

  • @TripleCreeper3
    @TripleCreeper3 8 місяців тому

    i understand it now, you are a beast

  • @klubis4935
    @klubis4935 10 місяців тому

    Just what i was looking for

  • @Liforus1
    @Liforus1 Рік тому

    The best explanation of interfaces. Thank you so much!

  • @Wishbone_Games
    @Wishbone_Games 2 місяці тому

    This saved my life

  • @AraragiAriel
    @AraragiAriel Рік тому

    Awesome! Answered exactly what I wanted to know

    • @JamesMakesGamesYT
      @JamesMakesGamesYT  Рік тому

      So glad to hear that! Hope this can help you with your game dev project

  • @xKingRognarx
    @xKingRognarx 2 роки тому

    That was really helpfull, thanks a lot :D

  • @Fyres11
    @Fyres11 Рік тому

    Yup I use a lot of Interfaces. Just my day night cycles system. use 3-4 different interfaces to control sun, moon, time, clocks, lights, etc. Even got a custom extension to find interfaces. For enemies. Mostly got IDamagable, IKnockable, IStunable.

  • @BFBStudio
    @BFBStudio Місяць тому

    Great Video!

  • @galileemason8813
    @galileemason8813 Рік тому

    great work !

  • @hernandonj
    @hernandonj Рік тому

    Awesome video. Thank you!

  • @petteriblomqvist8309
    @petteriblomqvist8309 Рік тому

    Huh. Interfaces aren't a new concept for me, but the way you managed to cram all relevant information about them into couple minutes just made everything click and suddenly I feel like I actually know how, when and why to use them for anything.
    Informative presentation, sticks to the topic, good pacing (a bit fast if the concept is all new and/or English isn't your native language, maybe). Overall verdict: Subbed.

  • @pistolerointergalactico9019
    @pistolerointergalactico9019 2 місяці тому

    Perfectly explained fast and clear

    • @der-Dritte
      @der-Dritte Місяць тому +1

      Better than offical unity video

  • @junaidywijaya
    @junaidywijaya 2 роки тому +2

    Most tutorial i found only show how to create the interfaces but not showing what it can really do and what is the benefit of using it, but this one is make it clear and show exactly what it can do, once you explaining about damaging in firstperson script, it make me realize how powerful interfaces can be if we know how to use it
    Edit : can you teach about event, action and delegates for next ? These are also a little bit tricky to understand,

    • @JamesMakesGamesYT
      @JamesMakesGamesYT  2 роки тому +1

      Glad the video helped and thank you for the suggestions!

  • @lucas8089
    @lucas8089 Рік тому

    Very good explanation. This video alone made my Unity skills go from 3/10 to 7/10. This is what I've not been doing

  • @farrukhkamal2315
    @farrukhkamal2315 Рік тому

    thats very nice. ima use it

  • @joshgilderdale99
    @joshgilderdale99 2 роки тому

    Awesome information here

  • @theemeraldend4302
    @theemeraldend4302 5 місяців тому

    Thank you, I am currently making something where the player can move in 2d, but needs to interact with stuff and need something that allows them to interact with a button without knowing what the interactable object does

  • @haliesnailie4541
    @haliesnailie4541 3 місяці тому

    this video explains interfaces better than my college classes lol

  • @tyb.c.4332
    @tyb.c.4332 Рік тому

    Gonna get my alt account to like this twice, great video!!

  • @GUYX10
    @GUYX10 10 місяців тому

    Thank you :)

  • @JayadevHaddadi
    @JayadevHaddadi 2 роки тому

    genius ❤️

  • @monkeyrobotsinc.9875
    @monkeyrobotsinc.9875 Рік тому +1

    btw just a tip: id slow down your talking a bit since your accent is pretty rough, and your voice is already kind of low in volume (try adding compression). i play all your videos at .75 speed and subs enabled, but still struggle to hear and understand you. im only trying to help because your content is very good. please keep it up.

    • @JamesMakesGamesYT
      @JamesMakesGamesYT  Рік тому

      Thanks, I appreciate the feedback! Pacing my speech is something I'm trying to work on

  • @damnverificationcode
    @damnverificationcode Рік тому

    Great video. Thank you (tiny bit slower to reduce pauses and moving backwards will be really nice if possible)

    • @JamesMakesGamesYT
      @JamesMakesGamesYT  Рік тому

      Thanks for the tip! Pacing is something I'm trying to get better at with my videos, so really appreciate the feedback

  • @AlexBlackfrost
    @AlexBlackfrost 2 роки тому

    "Nice one, James!"

  • @Eculeus007
    @Eculeus007 10 місяців тому

    If you want differnts damages so make this:
    public interface IDamageable
    {
    void Damage(float physicalDamage, float magicalDamage = 0f)
    }
    burnDamage , poisonDamage , how you like.
    Declare attributes for your Weapon. (PhysicalDamage, MagicalDamage)
    and use
    damageable.Damage(weapon.physic, weapon.magic)
    or only
    damageable.Damage( 0f, weapon.magic)

  • @facundocastro2807
    @facundocastro2807 Рік тому +1

    Then in the minute 2:44 you had made Damage() method in all the classes you want, then you say "hey raycast that object, if the ray collide with a GO that have a interface IDamageable then you say well, that obj have a interface, go and execute the Damage method that's declared in you Interface IDamageable" and how when add interface make a method required, then the interface will find the method damage on the specific object instance script and execute his public method Damage(), right?
    Sorry for my English, I'm trying to learn.

  • @TheNbc27
    @TheNbc27 2 роки тому +1

    Hello, I am trying to troubleshoot what seemingly should be a simple problem, but unfortunately I definitely didn't structure my scripts properly. It involves player skins, and addressables.
    What defines a local reference in unity? I asked a more in depth question elsewhere, and I was essentially told that all my local references would break, and then was told if it was prefab references they "should" break. I believe it was a typo and he meant wouldn't break.
    Thank you for any help!

  • @ikelos8190
    @ikelos8190 9 місяців тому

    could you also use composition (of classes) for some of these problems right?

  • @MaskedImposter
    @MaskedImposter Рік тому

    Interfaces are cool but I keep struggling to find a use case in my projects. I'm wondering if they might help in my buff system though... I'll need to review the code when I get the chance.

  • @nightyonetwothree
    @nightyonetwothree 2 роки тому

    I have tendency to use interfaces' names like ICanDoSomething or IHaveSomething (GetSomethingFunc/property), as I+able sounds as it's a target of an outer action.
    How bad these practice is?

  • @risingdagger5416
    @risingdagger5416 3 місяці тому

    this is good but how do i check if the collision is not in play, because I'm using a boolean in my code to check if the enemy is grounded or not using this interface method

  • @HansonJ
    @HansonJ 3 місяці тому +1

    GetComponent can retrieve Interfaces? I thought it could only grab actual components, makes the name misleading.

  • @johnsartain4160
    @johnsartain4160 8 місяців тому

    I'm still not sure if I need to be using interfaces or a base class on my current project

  • @zekiozdemir420
    @zekiozdemir420 2 роки тому

    thank you

    • @JamesMakesGamesYT
      @JamesMakesGamesYT  2 роки тому

      You’re welcome! Hope this helps you with your game project!

  • @lucasgrd4258
    @lucasgrd4258 Рік тому

    alright take my sub

  • @JPsonicACE
    @JPsonicACE Місяць тому

    that was really usefull and clear , im just not sure .. where do you write the Interface - > public Interface Idamageable { void damage(amount) }

  • @thedragonlegend6192
    @thedragonlegend6192 2 роки тому

    just to be sure do write your interface in a new script or in the case of the shooting game for example in the shooting game youi made would it be on your caracter script ?

    • @JamesMakesGamesYT
      @JamesMakesGamesYT  2 роки тому +1

      Good question! I prefer to define an interface in its own script file because I see it as a separate concept that can be used across different scripts

  • @angel_machariel
    @angel_machariel Рік тому

    Excellent example. Question:
    If an object is many things, such as IDamagable, IEnumerable, IPersistent, IWhateverable, IFlyable, ISwimable, then how would one go about that design wise? Surely deriving from many Interfaces must be a flawed approach?

    • @JamesMakesGamesYT
      @JamesMakesGamesYT  Рік тому +1

      Great question! Deriving from multiple interfaces isn't a bad design in itself, but it can lead to your class having too many responsibilities.
      For example, we could have a PlayerController class that implements IDamageable, IHealable, IFlyable and ISwimmable
      There's nothing wrong with implementing many interfaces likes this, but if your class gets too big, then it may make sense to split the class up based on the their concepts. In the above example we could split PlayerController into a PlayerHealth class that implements IDamageable and IHealable (the concept of adding and removing health) and PlayerMovement that implements IFlyable and ISwimmable (the concept of movement)
      I hope this answered your question

    • @angel_machariel
      @angel_machariel Рік тому

      @@JamesMakesGamesYT Oh wow, this makes sense to me. Thanks for taking your time to answer!

  • @_ace55
    @_ace55 Рік тому

    Hey, could you make a Video about "base classes" (mentioned at 3:13)

    • @JamesMakesGamesYT
      @JamesMakesGamesYT  Рік тому

      Thanks for the suggestion, I can look into it! Anything in particular you'd hope to understand better in the video?

    • @_ace55
      @_ace55 Рік тому

      @@JamesMakesGamesYT I'm relatively new so I don't know what base classes are at all! So what they are, how to use them properly and an example would be awesome!

  • @fv4202x
    @fv4202x 4 місяці тому

    I could do this with sub classes. Interfaces just makes things a little bit longer. Then why are they exist? I never felt like I really need interfaces.

    • @erikberg2893
      @erikberg2893 4 місяці тому

      He goes over that in the video. You're only able to inherit from one class. What if you want to give your class more functionality after the you you make the subclass to work with another system you already have in place?

    • @fv4202x
      @fv4202x 4 місяці тому

      @@erikberg2893 I changed my mind thank you

  • @irondolphin1559
    @irondolphin1559 9 місяців тому

    I just dont get how you can get the component of an interface when you cant put interfaces on gameobjects? Wouldnt your code technically look for an interface that isn't there

    • @irondolphin1559
      @irondolphin1559 9 місяців тому

      I mean if its finite as in no mono behavior of course.

  • @lee1davis1
    @lee1davis1 Рік тому

    The problem I see with interfaces is that they disregard the DRY principle.

  • @sayankabir7958
    @sayankabir7958 2 роки тому

    great video. although i think i need to watch it 5 times more to understand properly 🙃

  • @Johan-rm6ec
    @Johan-rm6ec 4 місяці тому +1

    Thank you for not using the word C.O.N.T.R.A.C.T. once!!!!

  • @watercat1248
    @watercat1248 Рік тому

    If understand correctly this inderfece method let you do something like damage for example
    Without need call spefic tag or script to do calling other script ECT if this inderfece method do what I thought the do you are live saver
    because before today I have plan to make multiple codes for damage the Player because in my game I have multiple damage source
    If understand correctly with this method I'm able to make the health for example IDamag and in my weapon damage output and I'm done not more work needs script the call other script and ECT
    I still I have not understand all the details but if the works in the way I understand is very useful thenks
    And no I didn't even know this inderfece system existed before today so never used before
    Ther is one thing I don't understand yet for Idamage
    method wean someone create health system the need input and output
    because not every object able to get damage and object take damage and object do damage can you able for example if the bullet touch the enemy Player lose 20 HP how the helth know that able to get damage for the IDamag ? And how the bullet able to know that when the tuch the enemy the do -20 HP ? For example I understand this method happeneds wean ever it's possible but still
    Can't you explain this topic to my
    What I'm trying to say is that the helth case like for example I tell you the one sure try to do damage in every area the able to do and the other get damage I need some way to combine those methods

  • @testservicesssss
    @testservicesssss 2 роки тому

    bad practise use -able, IDamagable bad interface. An INTERFACE in C# is a type definition similar to a class, except that it purely represents a contract between an object and its user. It can neither be directly instantiated as an object, nor can data members be defined. So, an interface is nothing but a collection of method and property declarations. The following defines a simple interface:

  • @fleabag631
    @fleabag631 2 роки тому

    Good video. I am currently using gameObject.SendMessage when I need to call common behaviours across different scripts (like the example in the video). What are the advantages of using interfaces over SendMessage?
    I can think of a few, such as messages not having return values (you get around that by sending a reference of the message sender) or the limitations on parameters (1 only, although you can send a class). Interfaces probably faster too, as SendMessage is a quite slow (usually not a problem if you use it sparingly).
    For those not aware, SendMessage allows you to call any function by it's name that is on any script on a gameObject. For example, in the Shoot script after the raycast you could call hitInfo.collider.gameObject.SendMessage("Damage", weaponDamage). Any shootable object has a Damage(float weaponDamage) function. Non shootable objects (like terrain) can just not have the Damage function and SendMessage will do nothing except use some CPU time (as long as you don't set the flag to send an error).

    • @JamesMakesGamesYT
      @JamesMakesGamesYT  2 роки тому +1

      Great question! I haven't worked with SendMessage but from before, but based on your description here's the difference I can see:
      You know there is a dependency between your current script and another script because you're calling damage on it using SendMessage. The interface approach makes that dependency explicit and more obvious whereas SendMessage hides it. The other issue you may have is if you change the name of the Damage method or the type of its parameters. In the case the code will compile and run, but the damage logic won't work correctly and it'll make it harder to track down the cause.
      In a real world example, imagine you need to tell your neighbour that his car is blocking your driveway. The SendMessage approach is you shouting at him from your window and hoping he can hear you. The interface method is knocking on his door and telling him directly.
      Hope this helps!

  • @richie4956
    @richie4956 4 місяці тому

    This might seem like a stupid question, but what are you doing in this line? EnemyTarget target = hitInfo.collider.GetCompoment(); is this a way to create an instance of a variable from another class? What is is required to do this? I understand the rat cast part I am actually only confused about the declaration of target