Using Interfaces in Unity Effectively | Unity Clean Code

  Рет қаралды 53,363

James Makes Games

James Makes Games

2 жыл бұрын

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 жыл бұрын
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!
@JamesMakesGamesYT
@JamesMakesGamesYT 2 жыл бұрын
Thanks for the recommendations, appreciate it!
@brettabraham
@brettabraham 2 жыл бұрын
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!
@GlitchyMF
@GlitchyMF 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
@Keanine
@Keanine 2 жыл бұрын
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!
@monkeyrobotsinc.9875
@monkeyrobotsinc.9875 Жыл бұрын
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 Жыл бұрын
Thanks for the kind words, I might try to do some more content on interfaces in the future
@saseb1060
@saseb1060 2 жыл бұрын
finally a video that has an easy and direct example and not something abstract
@JamesMakesGamesYT
@JamesMakesGamesYT 2 жыл бұрын
Glad you found the video useful!
@thedude110
@thedude110 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 жыл бұрын
Very clear and very well illustrated. I'm lucky to have subscribed!
@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
@RetroVemVideos
@RetroVemVideos Жыл бұрын
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!
@scull3208
@scull3208 7 ай бұрын
I can't express how much this 4 minute video has helped me. Thanks so much!
@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 !
@johnnyfatsacks5209
@johnnyfatsacks5209 6 ай бұрын
I never fully understood interfaces and this perfectly explains it. Thanks.
@junaidywijaya
@junaidywijaya 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.
@aurabeth1680
@aurabeth1680 11 ай бұрын
Very helpful and gave the WHY and HOW in a very digestible way. Thank you!
@williamwan188
@williamwan188 Жыл бұрын
Super short and clean tutorial. Thanks very much.
@lcb5651
@lcb5651 Жыл бұрын
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!
@flofdev
@flofdev Жыл бұрын
Wow this really opened my eye to so much more possebillities. Thanks
@IlaiyaBharathi
@IlaiyaBharathi 2 жыл бұрын
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
@JamesMakesGamesYT
@JamesMakesGamesYT 2 жыл бұрын
Glad the video was helpful!
@darkmattergamesofficial
@darkmattergamesofficial 7 ай бұрын
This is incredibly helpful. Thank you!
@tomasroch4905
@tomasroch4905 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 жыл бұрын
Thanks for the kind words! I've taken a bit of a break from KZbin but I'm working on more videos for the future!
@SelaMalka
@SelaMalka Жыл бұрын
Amazing video, thank you so much! Subscribed!
@salarmanan323
@salarmanan323 5 ай бұрын
damn such a great explanaition. definetely deserves a sub. thank you so much man :D
@okee
@okee 2 жыл бұрын
Short, quick, and helpful
@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
@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
@vaibhavjaiswal799
@vaibhavjaiswal799 2 жыл бұрын
Suddenly interfaces seems easy, really good explanation
@dontthinktoomuch7152
@dontthinktoomuch7152 9 ай бұрын
The best tutorials i've ever seen
@dreamisover9813
@dreamisover9813 Жыл бұрын
Short, and explains the problem as well as the solution very well!
@JamesMakesGamesYT
@JamesMakesGamesYT Жыл бұрын
Glad the video was helpful!
@klubis4935
@klubis4935 10 ай бұрын
Just what i was looking for
@Liforus1
@Liforus1 Жыл бұрын
The best explanation of interfaces. Thank you so much!
@JamesMakesGamesYT
@JamesMakesGamesYT Жыл бұрын
Thanks! Glad the video was helpful!
@TripleCreeper3
@TripleCreeper3 9 ай бұрын
i understand it now, you are a beast
@LuzuVlogsGamer
@LuzuVlogsGamer 10 ай бұрын
This was soo well made I would of believed you if you said that you worked for Unity :O
@overyonderstudios9779
@overyonderstudios9779 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!
@hernandonj
@hernandonj Жыл бұрын
Awesome video. Thank you!
@JamesMakesGamesYT
@JamesMakesGamesYT Жыл бұрын
Thanks Hernando!
@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
@pistolerointergalactico9019
@pistolerointergalactico9019 2 ай бұрын
Perfectly explained fast and clear
@der-Dritte
@der-Dritte Ай бұрын
Better than offical unity video
@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.
@haliesnailie4541
@haliesnailie4541 3 ай бұрын
this video explains interfaces better than my college classes lol
@xKingRognarx
@xKingRognarx 2 жыл бұрын
That was really helpfull, thanks a lot :D
@galileemason8813
@galileemason8813 Жыл бұрын
great work !
@joshgilderdale99
@joshgilderdale99 2 жыл бұрын
Awesome information here
@farrukhkamal2315
@farrukhkamal2315 Жыл бұрын
thats very nice. ima use it
@BFBStudio
@BFBStudio Ай бұрын
Great Video!
@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!
@GUYX10
@GUYX10 10 ай бұрын
Thank you :)
@tyb.c.4332
@tyb.c.4332 Жыл бұрын
Gonna get my alt account to like this twice, great video!!
@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
@monkeyrobotsinc.9875
@monkeyrobotsinc.9875 Жыл бұрын
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
@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.
@JamesMakesGamesYT
@JamesMakesGamesYT Жыл бұрын
Thanks! I'm glad the video was helpful
@JayadevHaddadi
@JayadevHaddadi 2 жыл бұрын
genius ❤️
@junaidywijaya
@junaidywijaya 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 жыл бұрын
Glad the video helped and thank you for the suggestions!
@AlexBlackfrost
@AlexBlackfrost 2 жыл бұрын
"Nice one, James!"
@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
@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)
@zekiozdemir420
@zekiozdemir420 2 жыл бұрын
thank you
@JamesMakesGamesYT
@JamesMakesGamesYT 2 жыл бұрын
You’re welcome! Hope this helps you with your game project!
@TheNbc27
@TheNbc27 2 жыл бұрын
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 10 ай бұрын
could you also use composition (of classes) for some of these problems right?
@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) }
@facundocastro2807
@facundocastro2807 Жыл бұрын
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.
@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?
@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
@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 жыл бұрын
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 9 ай бұрын
I'm still not sure if I need to be using interfaces or a base class on my current project
@HansonJ
@HansonJ 3 ай бұрын
GetComponent can retrieve Interfaces? I thought it could only grab actual components, makes the name misleading.
@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 Жыл бұрын
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 10 ай бұрын
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 10 ай бұрын
I mean if its finite as in no mono behavior of course.
@sayankabir7958
@sayankabir7958 2 жыл бұрын
great video. although i think i need to watch it 5 times more to understand properly 🙃
@lee1davis1
@lee1davis1 Жыл бұрын
The problem I see with interfaces is that they disregard the DRY principle.
@JamesMakesGamesYT
@JamesMakesGamesYT Жыл бұрын
Interesting, in what way?
@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
@Johan-rm6ec
@Johan-rm6ec 4 ай бұрын
Thank you for not using the word C.O.N.T.R.A.C.T. once!!!!
@simoncote4064
@simoncote4064 2 ай бұрын
he did tho twice XD at 2:30
@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 жыл бұрын
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
What are Interfaces? (C# Basics)
13:54
Code Monkey
Рет қаралды 200 М.
Dynamic #gadgets for math genius! #maths
00:29
FLIP FLOP Hacks
Рет қаралды 19 МЛН
Improve Your Unity Code with MVC/MVP Architectural Patterns
15:32
Better Coding in Unity With Just a Few Lines of Code
15:27
Firemind
Рет қаралды 301 М.
Using Interfaces in Game Development (Unity Tutorial)
7:00
Infallible Code
Рет қаралды 87 М.
How to Implement Blackboard Architecture in Unity C#
28:57
git-amend
Рет қаралды 7 М.
Unity Interfaces vs Abstract Classes - Part 1 - Interfaces
10:11
Jason Weimann
Рет қаралды 66 М.
Learn INTERFACES in 5 minutes | Unity
5:38
Root Games
Рет қаралды 3,7 М.
This Game Dev Tip Saved Me 137 HOURS!
2:56
James Makes Games
Рет қаралды 3,7 М.
Escape Nesting Hell - Do This Instead
2:48
James Makes Games
Рет қаралды 265 М.
11 Things You (Probably) Didn't Know You Could Do In Unity
13:49
Game Dev Guide
Рет қаралды 147 М.