Using Interfaces in Unity Effectively | Unity Clean Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
when we're writing code for our unity games we sometimes notice that we're duplicating code for very similar but unrelated scripts this can be a sign that you should be using interfaces in this video i'll show you what interface is how to use them and it benefits using interfaces in your unity projects let's get started the project we're working with is a simple first person shooter where we can shoot different objects looking at the code for each we have an enemy target with health that decreases every time it's shot a destructible cover object or break when it's shot and an explosive barrel that will explode when it's shot we also have a first person controller now there's a lot going on here but the main concern is in the shoot method we fire out a raycast from the center of the screen and if it's an enemy target we call hit if it's a destructible cover we call break and if it hits an explosive barrel we call explode this part might seem a bit odd there's a different method call for each object that we shoot if we want to add more objects in the game that we can shoot then we have to add more logic to the class for each object that we add the underlying problem here is we have common behavior across unrelated classes each object has something similar when the player shoots it but we're calling a different method for each one this is where we can make use of interfaces interfaces are often referred to as contracts because the enforcer class explores certain properties and methods here's another way you can look at this an interface lets you know what a class can do without having to know how it does it the first step is to find the common behavior between these classes we have three methods hit break and explode a more generic method could be shoot but this behavior could also be common to other actions like throwing a grenade or performing a melee attack so let's go with the more generic name damage so what does an interface look like here's an example for the interface that will allow the class to take damage called eye damageable some common conventions for naming interfaces are to add the letter i at the start and to add able at the end but these are both optional it's very similar to declaring a class except for a few differences we use the keyword interface instead of class we don't specify axis modifiers on our methods or properties since by default it should be public finally our methods don't have a body just a semicolon this is because the interface is defined what the class can do not how it will do it that's up to the class that will implement the interface speaking of which let's implement this interface in our three damageable object classes starting with enemy target we add a colon next to the class name followed by the interface name i damageable now add the damage method we define in the interface and inside it we'll just call hit if you don't do this you'll get a compilation error remember an interface is a contract if you don't implement the methods defined in the interface then you're breaking that contract and that's all there is to it so let's quickly repeat that for the destructible cover and the explosive barrel classes the next step is to change our first person controller in the shoot method find the code where we are getting the corresponding components since all our classes now implement eye damageable we can remove all these lines and just call get component eye damageable and it'll find the component that implements the interface now the first person controller doesn't know the specifics about all the objects it can shoot instead we're saying if i hit something i can damage then damage it i don't care what the object is i don't care what happens when i damage it i let the other class worry about that this helps to break the dependencies between these scripts which means we can change one without breaking the others but you might ask can't i just use a base class and you can but remember you can only inherit from one base class in c sharp the benefit to using interfaces here is you can implement multiple interfaces on one class in the example on screen we now have a grenade that we can throw and he'll explode after a few seconds but if we shoot it it'll explode straight away the grenade class implements two interfaces eye throwable and eye damageable two completely unrelated concepts a base class wouldn't work here because not everything you can throw can be damaged and not everything you can damage can be thrown this is because a base cost defines what a class is whereas an interface defines what class does when you design your classes in your scripts try to look at them in terms of their behaviors and abilities rather than what they can be described as you don't care if it's an enemy or a barrel you care if you can shoot it you don't care if it's a sword or a portion you care if you can store it when you start to look at the objects in your game like this you'll find it much easier to identify these smaller modular interfaces and when you do interfaces like this will help you break dependencies between scripts let you add new mechanics faster and keep your code clean let me know in the comments for using interfaces in your game projects and as always if you found this helpful check out the other videos on screen leave a like and subscribe for more
Info
Channel: James Makes Games
Views: 48,987
Rating: undefined out of 5
Keywords: james makes games, unity clean code, interface in c#, unity3d interfaces, clean code, unity3d interface c#, unity3d interface, unity 3d interfaces, unity3d c# interface, interfaces in unity, how to use interfaces in unity, interfaces in unity c#, unity interface tutorial, unity interfaces c#, unity interfaces, c# interface explained, c# clean code, interfaces, c# interfaces
Id: Zzklpdvue3A
Channel Id: undefined
Length: 4min 22sec (262 seconds)
Published: Fri Feb 25 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.