Using Interfaces in Unity Effectively | Unity Clean Code

Поделиться
HTML-код
  • Опубликовано: 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

Комментарии • 113

  • @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!

  • @thedude110
    @thedude110 2 года назад +2

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

  • @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

  • @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!

  • @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

  • @junaidywijaya
    @junaidywijaya 2 года назад +2

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

  • @scull3208
    @scull3208 7 месяцев назад

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

  • @jean-michel.houbre
    @jean-michel.houbre 2 года назад +1

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

  • @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 !

  • @Stromhammar
    @Stromhammar 2 года назад

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

  • @--NLS--
    @--NLS-- 9 месяцев назад

    It's very very rare to find tutorials that gets to the point, explains it well, but with clean looking code!!! So many tutorials have the ugly nested code while trying to explain concepts that can be hard to grasp at first.

  • @johnnyfatsacks5209
    @johnnyfatsacks5209 6 месяцев назад

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

  • @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

  • @foreducation408
    @foreducation408 11 месяцев назад

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

  • @saseb1060
    @saseb1060 Год назад +2

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

  • @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.

  • @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!

  • @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 RUclips but I'm working on more videos for the future!

  • @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

  • @flofdev
    @flofdev Год назад

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

  • @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

  • @vaibhavjaiswal799
    @vaibhavjaiswal799 2 года назад

    Suddenly interfaces seems easy, really good explanation

  • @darkmattergamesofficial
    @darkmattergamesofficial 6 месяцев назад

    This is incredibly helpful. Thank you!

  • @linksys8654
    @linksys8654 Год назад

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

  • @salarmanan323
    @salarmanan323 4 месяца назад

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

  • @SelaMalka
    @SelaMalka Год назад

    Amazing video, thank you so much!
    Subscribed!

  • @okee
    @okee 2 года назад

    Short, quick, and helpful

  • @dontthinktoomuch7152
    @dontthinktoomuch7152 9 месяцев назад

    The best tutorials i've ever seen

  • @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

  • @Liforus1
    @Liforus1 Год назад

    The best explanation of interfaces. Thank you so much!

  • @dreamisover9813
    @dreamisover9813 Год назад

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

  • @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!

  • @TripleCreeper3
    @TripleCreeper3 8 месяцев назад

    i understand it now, you are a beast

  • @hernandonj
    @hernandonj Год назад

    Awesome video. Thank you!

  • @klubis4935
    @klubis4935 9 месяцев назад

    Just what i was looking for

  • @pistolerointergalactico9019
    @pistolerointergalactico9019 Месяц назад

    Perfectly explained fast and clear

    • @der-Dritte
      @der-Dritte Месяц назад +1

      Better than offical unity video

  • @xKingRognarx
    @xKingRognarx 2 года назад

    That was really helpfull, thanks a lot :D

  • @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

  • @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.

  • @Wishbone_Games
    @Wishbone_Games 2 месяца назад

    This saved my life

  • @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

    • @JamesMakesGamesYT
      @JamesMakesGamesYT  Год назад

      That's a great testimonial! :D Glad the video helped you!

  • @joshgilderdale99
    @joshgilderdale99 2 года назад

    Awesome information here

  • @tyb.c.4332
    @tyb.c.4332 Год назад

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

  • @BFBStudio
    @BFBStudio Месяц назад

    Great Video!

  • @galileemason8813
    @galileemason8813 Год назад

    great work !

  • @farrukhkamal2315
    @farrukhkamal2315 Год назад

    thats very nice. ima use it

  • @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.

  • @haliesnailie4541
    @haliesnailie4541 3 месяца назад

    this video explains interfaces better than my college classes lol

  • @theemeraldend4302
    @theemeraldend4302 4 месяца назад

    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

  • @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!

  • @GUYX10
    @GUYX10 10 месяцев назад

    Thank you :)

  • @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

  • @JayadevHaddadi
    @JayadevHaddadi 2 года назад

    genius ❤️

  • @AlexBlackfrost
    @AlexBlackfrost 2 года назад

    "Nice one, James!"

  • @zekiozdemir420
    @zekiozdemir420 2 года назад

    thank you

    • @JamesMakesGamesYT
      @JamesMakesGamesYT  2 года назад

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

  • @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

  • @ikelos8190
    @ikelos8190 9 месяцев назад

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

  • @lucasgrd4258
    @lucasgrd4258 Год назад

    alright take my sub

  • @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)

  • @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!

  • @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.

  • @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

  • @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.

  • @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

  • @johnsartain4160
    @johnsartain4160 8 месяцев назад

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

  • @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?

  • @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!

  • @HansonJ
    @HansonJ 3 месяца назад +1

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

  • @_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 3 месяца назад

      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 3 месяца назад

      @@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 3 месяца назад +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