Unity Interfaces vs Abstract Classes - Part 1 - Interfaces

Unity Interfaces vs Abstract Classes - Part 1 - Interfaces

Jason Weimann (GameDev)

7 лет назад

67,246 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@Spyderislive
@Spyderislive - 15.02.2024 21:01

at last i understood how to do interfacing thank you so much

Ответить
@hailelam4112
@hailelam4112 - 14.05.2023 18:00

Thank you so much

Ответить
@Giant_Swing
@Giant_Swing - 21.03.2023 22:29

Before I learned interfaces, I just had a different component that was a monobehaviour that I'd add to any object I wanted to be able to take damage and then invoke an action I subscribed to in the main script of the enemy or whatever it was, in the end it worked really similar to this just that I would get that component in the raycast instead of the interface.

I know that having too many monobehaviours is bad for performance but other than that is there any benefit to this? The thing I liked about my previous approach was that I could just enable and disable whenever I wanted that 'take damage" component to test different behaviors which I don't think I can do with this interface approach without having to start adding if elses, am I missing something here? Thanks

Ответить
@alicanted
@alicanted - 15.03.2023 10:53

So ITakeDamage is considered a component?

Ответить
@FlipYourLearning
@FlipYourLearning - 21.09.2021 00:26

Oh, this was so helpful. After a while this video finally made it click for me: interfaces can be used (among other things, probably) when you want to call a certain method from a certain object but you don't know what exact script of the object will have the method.

I'm imagining a fireball that could damage a player by calling a method on its health script, make something explode by calling a method on one of its scripts, and make something else simple catch fire but not be destroyed (by calling a different method on a different script of that burnable object).

With interfaces, I simply choose one script, already on the objects that could react to the fire (or alternatively make a new script that I would attach to each object), and make that script inherit from an interface (like IFireReactive), and make that interface have a method (for example Reaction). Then, each script inheriting the interface defines what it does when the method is called. In my fireball, upon hitting anything, I get the component IFireReactive of whatever it hit and call its reaction method. Then each object does whatever it was coded to do.

That way I can have one script for all things with fire and one script for all things reactive to fire, with no internal conditionals. The fireball or anything else with fire doesn't need to figure out what is hitting to make it react a certain way or call the right method from the right component, and the script that will react to the fire doesn't need to figure out what object it is in to decide what it does.

Basically, designing with interfaces in mind instead of without using interfaces can save you code complexity and trouble later on when you want to expand your systems or start changing things.

Ответить
@TheKr0ckeR
@TheKr0ckeR - 13.09.2021 13:57

Hey, thanks for great guide! What would we do in best practice if we wanted to damage spesific for example "5 damage to car" and "2 damage to barrel"? We are here wont be able to change by it. Maybe we can increase their hp in their class. But what would be the other way?

"
ITakeDamage damagable = hitInfo.collider.GetComponent<ITakeDamage>();

if (damagable != null)
{
damagable.TakeDamage(5); // here we damage all Idamagable interfaces
} "

Ответить
@MikeArcuri
@MikeArcuri - 04.08.2021 03:23

Great tutorial! Q: do you see this pattern as a good alternative to using events for things like damage? Or are event systems preferable? Or would you use both interfaces and events in the same game?

Ответить
@kiwiboysl
@kiwiboysl - 18.05.2021 12:13

I've become accustomed to using something more like this. ``` if( hitInfo.collider.TryGetComponent(out ITakeDamage damagable)) {//Do stuff with damagable} ``` This way you don't have things allocating in the Editor when the requested component does not exist.

Ответить
@zolan4277
@zolan4277 - 27.02.2021 04:25

I literally snorted when you tried to make the health variable public and physically couldn't force yourself to do it.

Ответить
@chadamitecheckoutredpillpl2641
@chadamitecheckoutredpillpl2641 - 09.02.2021 23:12

its all so tiresome

Ответить
@richardosborn3810
@richardosborn3810 - 01.01.2021 03:23

Hey Jason, I've tried this in my own project, but it doesn't seem to work. I mean, I watched it work in your project, but I can't seem to get GetComponent to work on Interfaces in my project. Has anyone confirmed this actually works? (I know my raycast is hitting the object I want, but the GetComponent<>() call always returns null).

Ответить
@Mogo-jan
@Mogo-jan - 30.09.2020 16:45

If an interface had multiple methods, they would all need to be implemented right?

Ответить
@ZeroSleap
@ZeroSleap - 08.08.2020 16:11

I like to think of interfaces,as "Declaring" what they do,for example ITakeDamage if you put this interface in any class it says "Hey i take damage man!"

Ответить
@kilianreisinger823
@kilianreisinger823 - 19.06.2020 23:16

I finaly understand Interfaces. thanks.

Ответить
@benzenatizineeddine7816
@benzenatizineeddine7816 - 25.03.2020 08:40

i'll die to know how monobehaviour works

Ответить
@PicnicsPete
@PicnicsPete - 10.03.2020 16:54

Really clear explanation. Thanks for doing these.

Ответить
@phambaoha170
@phambaoha170 - 17.01.2020 20:16

nooooooooooo

Ответить
@UltramarineAfterglow
@UltramarineAfterglow - 31.12.2019 23:21

I have a cold. it's a few hours before the New Year 2020. i'm all alone and learing about Interfaces. Live is good :)

Ответить
@F3ND1MUS
@F3ND1MUS - 26.12.2019 12:48

ty

Ответить
@aldigangster123
@aldigangster123 - 15.10.2019 09:38

You did, what for some reason I could not find anyone else to explain properly (not even my paid Udemy courses).

How to properly call something on another object, who implements the interface, without having to know about it.

Thank you so much! Subscribed.

Ответить
@alialacan7774
@alialacan7774 - 03.10.2019 15:30

Great tutorial thank you!

Ответить
@punixerz
@punixerz - 20.06.2019 15:18

I can only subscribe once bro! Thanks for your work.

Ответить
@heyjeanwtf
@heyjeanwtf - 01.06.2019 21:58

You just saved my life. I ran through the whole internet trying to find a way to "access a script without knowing its name", and Interfaces seem to be the way! I'm making a turn-based strategy game and all I wanted to do is to grab into the GameManager the script of whichever gameobject I click on. Thanks a lot!

Ответить
@sakari.niittymaa
@sakari.niittymaa - 09.04.2019 16:00

Perfect! Thank you!

Ответить
@lux2640
@lux2640 - 27.02.2019 19:15

You teach really, really well. Thanks for everything, man.

Ответить
@gdk870
@gdk870 - 03.01.2019 20:06

Hello there,
Great video. First time it clicked for me. So where can i get more on this programming related topics? Is this video part of a series/playlist? I'd like to dig in some more. Cheers.

Ответить
@RetroGamingClashOfClans
@RetroGamingClashOfClans - 17.12.2018 00:32

But i can have a script called "Damagable" (with a take damage method in it) a normal class and add to all the damageable objects and check if that script exists and do GetComponent<Damageable>().TakeDamage();

Ответить
@mintydog06
@mintydog06 - 12.12.2018 22:56

Thanks for this video, brilliant, and you work so quickly as well. I wish I knew about Interfaces a year ago, might have saved me some hassle haha. I'm going to implement some now and hopefully improve my game. I really should learn more C#.

Ответить
@spiral9316
@spiral9316 - 11.12.2018 07:39

Watching your videos is like, the first time I went to United States.. everything was like rich and abundant..(compared to my country)

You are cool.

Like being on a dessert and then I arrive on the oasis.. feels good man.

I mean this; i have watched so much tutorials.. Informations and oh my God.. At last i get it!!!

Thank you!
from my hearth of hearths !! Appreciated

Ответить
@spiral9316
@spiral9316 - 11.12.2018 07:35

A light shines forth on the horizon. As i understand this problematic concept.

Sir.. I tip my fedora; and take damage with a smile.

Ответить
@FuzzyDPozzy
@FuzzyDPozzy - 29.11.2018 21:06

@Unity3dCollege
thank you sir for this incredible video sometimes you make really incredible videos like this, i just want to say after all this words you took your time to read even though i don't know what i am trying to say is that i have no other words to say how grateful and thankful i am for coming across to this awesome epic ultra awesome tutorial about interfaces , i just want to say a big big big thank you !!!

Ответить
@huseyin.goktas
@huseyin.goktas - 11.09.2018 14:04

It is super amazing.

Ответить
@firnekburg4990
@firnekburg4990 - 19.07.2018 18:20

You're the best !
Thank you !

Ответить
@CanaldoCaverninha
@CanaldoCaverninha - 23.05.2018 14:20

U ARE UNITY GOD MA-BOY !!

Ответить
@dago6410
@dago6410 - 19.04.2018 00:38

this dood looks like a bum but he is SO mfing smart

Ответить
@brianpangburn5573
@brianpangburn5573 - 05.04.2018 13:31

Your videos are completely changing the way I do things for the better. When I can afford it, I want your master classes!!

Ответить
@mykilpee
@mykilpee - 07.03.2018 09:21

As a massive fan of abstraction in my code you have taken possibly 2 or 3 classes of look-ups and simplified everything and saved me days of work. Thank you so much!
I'm a dangerous but knowledgeable coder, so in a month after changing my code around and adding more features if you can show me how I can create a pointer to a char and cast that to a bool in C# OR even tell us how we can build and call some C++ functions (some from other C++ libraries) I can create a feature to make some fun as hell saved games in some fun and exciting formats.

Ответить
@lee1davis1
@lee1davis1 - 01.02.2018 18:50

I choose to just create a monobehav. script that handles damage and drag it to any gameobject I want to have health. Thereby eliminating the need to add interfaces to every script .

Ответить
@antimuffin7
@antimuffin7 - 27.10.2017 12:26

Thanks for the explanation--working on sorting out these definitions and your video was a big help.

Ответить
@imaginationrabbit
@imaginationrabbit - 14.09.2017 08:29

Great tutorial- thank you!

Ответить
@Killer_Nads
@Killer_Nads - 11.09.2017 22:54

Fantastic Tutorial this!!

Ответить
@edman3d593
@edman3d593 - 11.09.2017 17:53

As someone who has watched shit loads of tutorial videos about everything, this is a fresh new channel taking on some less popular topics (not necessarily this topic but others). Videos are great, my request for a video is adding high quality lighting to a 2D game in Unity.

Ответить
@danielrousseau6541
@danielrousseau6541 - 11.09.2017 10:36

you just changed the game for me :D

Ответить
@burchmore5000132
@burchmore5000132 - 11.09.2017 04:57

The one thing I consistently have the most trouble with in Unity is the overall architecture of my code. Everything ALWAYS ends up becoming a tightly coupled monolithic structure that is impossible to extend beyond a certain point.

Here is an example of the kind of problem I end up encountering:

- UI. Should my UI 'observe' the game logic it's representing and update itself as it's own thing (and have the game logic/objects be oblivious to the UI), or should the UI be directly updated from the game objects that it is supposed to represent. I imagine it would be the former, but then I run into issues, like; how do I ensure the UI is aware of game objects that get instantiated or altered at runtime AND require UI representation without those game objects actively updating the UI or being at least somewhat aware of the UI in order to notify it that it exists.

Inevitably what ends up happening is my UI gets controlled by the game objects in order to solve the 'awareness' issue and to satisfy all the different types of UI behavior I want, and then I end up with bugs that relate to the UI being controlled from multiple places (eg. if I have other 'global' scripts controlling UI visibility based on the game state, as well as UI being controlled by the individual game objects it's representing).

Do you have any general tips or pointers (tutorials etc) for info on how to solve these more broader 'architectural' type problems?

Ответить