What are Interfaces? (C# Basics)

What are Interfaces? (C# Basics)

Code Monkey

4 года назад

204,269 Просмотров

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


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

LeoCreepSneak
LeoCreepSneak - 28.09.2023 05:53

What is a struct?

Ответить
Antti V
Antti V - 22.09.2023 23:22

I thought that components need to be separate "things" that are added to a game object and we can see them via inspector. The bullet checks for a component and finds the interface IDamageable even though the object does not have the script added in a way that we can see it on the inspector.

Is Monobehaviour also a component then?

Ответить
slowfuse
slowfuse - 14.09.2023 02:57

i could watch this video 100 times and not understand what the point of this is

Ответить
HeavensField
HeavensField - 13.09.2023 04:56

So it’s like a Base class that can call the function that it’s child uses without having to declare the child class itself?

Ответить
glasgood
glasgood - 01.09.2023 15:50

Excellent, was slightly puzzled by Interfaces and with your clear explanation it now makes sense.

Ответить
Burak Cenk Adalan
Burak Cenk Adalan - 21.08.2023 10:36

I started really few days ago and every topic I watched , really nice explanation and simple examples.Thank you for prepared these .

Ответить
Mr Guwee
Mr Guwee - 13.08.2023 10:32

funfact: if you watch this at 1.25x speed it sound like his newer tutorials :D

Ответить
qwe rtz
qwe rtz - 11.07.2023 23:41

that is insanely op, i could dodge so much code overhead xdd

Ответить
[18 - ASTHERISK] Junaidy Wijaya
[18 - ASTHERISK] Junaidy Wijaya - 28.06.2023 19:42

Just to say thanks and tell you, this way of teaching is really good for many reasons, not only explain the definition but also showing a real case scenario and after watching and searching other videos, this one is still the best one and easiest to understand, i do have watched your explanation about events and also delegates, but i forgot it sometimes and keeps coming back, so just want to say thanks for all the works you do, and i hope you found your success in your current project, and also i want to ask about planning for the future, any new paid course in udemy, i really wondering how to create the chart in your blueprint steam game, and how to manage those data, I've tried to make something similar but ended up a mess, i knew you are busy these days, and yeah I'm just mainly want to say thanks. 😅

Ответить
Jakon
Jakon - 27.06.2023 10:08

Great Video, i only have one Question. Why do you use the TestInterface Method with the Interface as a parameter and not use: myClass.TestFunction() ?

Ответить
bathtubanarchy
bathtubanarchy - 11.06.2023 08:15

This was very helpful, thank you.

Ответить
jud.Su.5 developer
jud.Su.5 developer - 29.05.2023 12:35

If i have pistol and i call damage () and it take 30 damage and i have sniper if i called damage it take same damage should i make function like this damage ( damage amount) to make each one take different damage?

Ответить
Mikhail Humphries
Mikhail Humphries - 27.05.2023 23:35

Is the interface only there to say that you have to implement the functions from the inherented interface(giving error message)?

Ответить
Tigran Avagyan
Tigran Avagyan - 25.05.2023 20:18

Thank's a LOT for this video!!! 👍

Ответить
jud.Su.5 developer
jud.Su.5 developer - 14.05.2023 15:48

Wow 🤩 ❤

Ответить
Pituivan
Pituivan - 10.05.2023 18:14

código mono

Ответить
Kerbalette
Kerbalette - 07.05.2023 01:35

Great video. Does unity support a C# version now that allows interface method implementations?

Ответить
Water Cat
Water Cat - 06.05.2023 15:21

because i have try multiple times to learn Interfaces we not luck do you able to shared the Interfaces codes to my ?

Ответить
P'KTHERX'TON'
P'KTHERX'TON' - 27.04.2023 19:37

/Z'({mvk(F'')shld M'''tx(XADT''')RvkxL'''(VARs mrkz')HUD/s dbl Lt'})/
/> CRTS' ROKtts' TRPDZOS' ++- X'-ZWTTS' battel'Plaa'Tz' || iWVxchmz'''/
/> int= cg AVN'''BZ' KOVS' VOLX'(tM''') RMZ'- 1/

Ответить
Stellar
Stellar - 10.04.2023 14:08

Interfaces look so easy to implement, I'm definitely gonna use them more and write cleaner code with them.

Ответить
Blu3W4r10Ck
Blu3W4r10Ck - 09.04.2023 11:10

Every other tutorial explains them as just a contract that does nothing. Thank you for explaining an actual use case.

Ответить
Gozeraye
Gozeraye - 06.04.2023 02:14

Sometimes with your tutorials I watch like "What's he doing? Why are you writing it like that?? What the hell even is-OHHHHHHHH SHIT"

Ответить
Roman Arriaga
Roman Arriaga - 01.04.2023 19:47

I've never implemented interfaces in Unity (but I do use an awful lot of protocols in Swift, same thing, different names).
I'm wondering tho, what are some technical advantages and disadvantages of interfaces vs delegates?

In my case, I have a class called TakeDamage, which has a delegated event called takeDamage, then everything that could be damaged, for example a script called health for most NPCs include the TakeDamage component and then subscribe to the takeDamage event, or for a breakable barrel, I do the same, add the component and subscribe to it.

Then I just need to check if there's a TakeDamage component and call its function to take damage.

So other than having to manage subscriptions manually, is there an advantage/disadvantage compared to implementing interfaces?

Ответить
Devdog7
Devdog7 - 29.03.2023 11:43

To help understand the concept of interfaces:

Interfaces are like middlemen for class dependencies. You don't have to worry about breaking dependencies when you change or switch out a class it depends on as long as it implements the same interface. The dependency just needs to know how to use the interface to get what it needs. It doesn't care how that output is produced as long as it works. When a class has implemented an interface, you know what methods to expect from it and if you ever delete one of the methods it will throw an error to ensure the agreed upon methods are being used (errors are caught immediately at the source instead of at runtime wondering what happened).

Analogy:

A DVD player and a blue ray player work differently yet expose the same interface (play, stop, fast forward, etc.). This way, the user doesn't have to know what is going on inside the machine. As long as they understand the interface, they can get the desired output (the video). This way they can switch out a DVD with a blue ray player and still get what they want.

Ответить
keeteir
keeteir - 27.03.2023 15:01

Found this channel, I am exited to learn new consepts. Seems like this guy is quite good about explainign things, I am sure it is not easy task to explaing how to write a code for a some codemonkey like myself XD

Ответить
SxAde: Lost Levels
SxAde: Lost Levels - 10.03.2023 07:15

Hi! I would like to know if there are any differences between using interfaces vs a generic component that implements UnityEvents that I can attach to any GameObject that requires that system or function.

For example, I made a generic Health component with a public TakeDamage() method that triggers a UnityEvent whenever damage is received, and it also triggers another one when it reaches zero health. This way I can reuse it in any other GameObject that requires the exact same health system and implement their own unique behavior by subscribing to the onDamageReceived and onDeath UnityEvents.

I found this method simple to understand, implement, reuse and modify, but I wonder if there are any performance or architecture advantages by using interfaces.

I would highly appreciate if anyone could clear this out.

Cheers!

Ответить
Water Cat
Water Cat - 27.02.2023 19:31

the interface is confusing no matter how many videos i show and not matter how trying to the have stuff that makes 0 sense to my

ther only way this system looks useful for the example you show is if you wand to open doors and colse the lights ect in few words the use or interact with the environment

because from what i understand what Interfaces do its to apply the fraction (the void) the have those contacts and to work no matter for how many the are

the have multiple problem's wean some use Interfaces for players health though at least in the example you show me

1. the damages is predefined that is terrible for helth system,
in most cases including the game im try to create the have multiple damage sources and each damage have the own values and the own way to dealing damage,
for example not every weapon in my game dealing the same damage and not every damage in my game is on trigger enter
2. why to use interface to dealing predefined damages and limit your self with all thos contracts,
instead to add on the on wean the bullets enter to dealing this predefined damage ?

in my opinion at least in the example you show me interface is not necessary and created health system with predefined is terrible idea

Ответить
BeerHuntor
BeerHuntor - 21.02.2023 02:05

Could you do an update to this video using the charp 8 default implementations please? I am trying to figure out how they work and how they can be used but im struggling atm :)

Ответить
Mrityunjay
Mrityunjay - 19.02.2023 09:22

So this is kinda like generics for classes?

Ответить
SaNdiP_169
SaNdiP_169 - 04.02.2023 15:31

i have fear to learn interfaces but when i watch your video then its gone, and i also started to loving interfaces as well as your flow of teaching that, thank you so much sir

Ответить
kozavr
kozavr - 23.01.2023 00:59

well with only 1 function in the interface it's easy - you just call that function. What if it has 3, 5, 10 functions and you have to implement them all?

Ответить
Erixperimentation
Erixperimentation - 20.01.2023 16:07

With interfaces it isn't even "inherit" anyway, you implement interfaces, not inherit from them. C# is still only a single inheritance language, but it allows you to get around this limitation with it by implementing multiple interfaces.

Ответить
David Varga
David Varga - 16.01.2023 14:38

For the first 9 minutes of the video I was clueless of what was happening. Wondering why would I even use this or how. Then you started the example and it clicked. This is exactly what I was looking for. Thanks

Ответить
Treplacon Studio
Treplacon Studio - 28.12.2022 17:09

I love this video. As a game artist trying to learn C# I need to see how my code works and what it is capable of, the example with the 3 different objects truly creates an Aha moment in my brain!

Ответить
VLADI BVLGARI
VLADI BVLGARI - 25.12.2022 17:54

Oi, good example of usage of an Interface.
This "GetComponent" method, is that a pattern or something? I like how how this plug-in method works. Has it been covered on this channel?

Cheers!

Ответить
Nhan Dao Siu
Nhan Dao Siu - 19.12.2022 19:52

Hi there, i just love how simple and fast with all of your tutorial, until this i got my unity studies done smoothly thanks to you, but when i try to put a Interface file to any GameObject it just say that i cant put abstract class onto it, is this new Unity feature now? i did search on google but there arent any topic about it, thanks alot!!

Ответить
Vahid Seyidov (feel9s)
Vahid Seyidov (feel9s) - 13.11.2022 14:05

just cool, into the unity, nice!🙌🏼

Ответить
MehdiDev
MehdiDev - 06.11.2022 19:20

best video ever on interfaces ,thank youuuuuuuuuuuuuu

Ответить
WoCraft Gaming
WoCraft Gaming - 26.10.2022 06:25

I spent the last 2 hours searching the web for how to see if an object contains a interface of a particular type similar to what you showed except through ray casting. After watching your video I confirmed I was executing my code correctly and it should have been working the way I thought it would have (for example being able to do TryGetComponent<IInteractable>) When it didn't work I thought maybe you cant do that since its not the classes direct name but you confirmed my thought that you can. Then I noticed it was because I had a box collider on the object and not a box collider 2D. (Im making a 2D game) And that's how I wasted 2 hrs of my life.

Ответить
Jason_ S
Jason_ S - 22.10.2022 13:19

U BEST!!!

Ответить
Brian Wilson
Brian Wilson - 16.10.2022 23:25

beautiful, thanks

Ответить
Grizzly Bear
Grizzly Bear - 16.10.2022 15:54

Mlk, se pá que o canal foi hackeado

Ответить
Spectral Space
Spectral Space - 12.10.2022 10:20

Thank you!!!!!!!!!!!!

Ответить
stb NY444
stb NY444 - 11.09.2022 15:24

Interfaces still elude me. Couldn't you just use a regular class and on bullet hit, get a reference to what collider you hit and then call gameObject.Damage() if the function exists?

Ответить
Empress Redbird
Empress Redbird - 01.09.2022 00:33

very cool feature

Ответить
AAA
AAA - 22.08.2022 01:07

C#
- White background
- "Function" instead of "Method"
- K&R bracket formatting.

What?!

Ответить
Umut Kaan Özdemir
Umut Kaan Özdemir - 16.08.2022 04:47

Thanks!

Ответить