How to... Cast Vs Interfaces

Поділитися
Вставка
  • Опубліковано 4 чер 2024
  • How to know when to use cast or interface? Is cast or interface better? What does casting do? How do interfaces work.
    Welcome to How to... a snack-size video for a snack-size question in Unreal Engine 4.
    SUPPORT ME
    Patreon I / ryanlaley
    Buy Me a Coffee I buymeacoffee.com/RyanLaley
    Donations I paypal.me/ryanlaley
    PRIVATE 1-2-1 SESSIONS
    Email me at support@ryanlaley.com for more information and rates, or visit www.ryanlaley.com/learn-games-...
    JOIN THE COMMUNITY
    Discord I / discord
    FOLLOW ME
    Twitter I / ryanlaley
    Facebook I / ryanlaleygames
    Instagram I / ryanlaleygames

КОМЕНТАРІ • 59

  • @problemchild959
    @problemchild959 3 роки тому +73

    you kinda forgot another VERY useful feature of Interfaces. You can call a function without loading all reference chains. a prime example of this is the Game Mode. if you cast a Game Mode and it references 20 other things, it loads all those references via a Cast. calling an Interface function on the Game Mode does not load all the references.

    • @TrojanLube69
      @TrojanLube69 3 роки тому +1

      It’s obvious.

    • @EveryGoodWorkInc
      @EveryGoodWorkInc 3 роки тому +34

      I did not know this - thank you so much for sharing, this is very helpful.

    • @problemchild959
      @problemchild959 3 роки тому +42

      @@TrojanLube69 actually its NOT obvious, especially to people new to unreal, if you read half the comments you will see just how NOT obvious it is.

    • @KillerSneak
      @KillerSneak 3 роки тому +1

      Yet casting is still faster than an interface. So performance wise it really doesn’t matter (casting is still 4 to 10 MicroSeconds faster than calling an interface so the performance is next to nothing)The only thing that makes a interface useful is that you don’t need to know anything about the actor or object that will use the interface.

    • @TrojanLube69
      @TrojanLube69 3 роки тому +2

      @@KillerSneak I would compare it similar to c++ calling Find actor with tag vs find actor class. It depends on your project complexity.
      Edit: but the point still stands, when you have a multiplayer game you don’t want too many unnecessary information because it will hault the bandwidth or even in single-player having unnecessary information loaded in memory.

  • @omegablast2002
    @omegablast2002 3 роки тому +18

    The only thing that I see was missing in the video, you hooked up the interface call from that previous cast... Which could lead people to think that they still need to do the cast... When you're using interfaces you don't ever really need to cast to anything you just send an interface message and if it has a matching interface it'll interact.
    It's also important to note that when you're calling that interface function that you choose the one that says (message).

    • @rolferikson
      @rolferikson 3 роки тому

      Ok with that I understand the difference. Because if you have to cast you can use Custom Event to get the same result. Is Casting expensive or why should I use interface?

    • @omegablast2002
      @omegablast2002 3 роки тому +7

      @@rolferikson casting too much can end up being expensive, you generally want to avoid casting in a tick function. A couple cast in begin play are usually fine unless you spawn tons of the same actor.
      You want to use interfaces to avoid creating dependency...with interfaces you can add the interface to many actors, and whenever something calls a function of a interface it basically just saids "hey random actor i dont know anything about, do you know how to do the 'my cool dance function'? If you do, than do my cool dance...you implement the event 'my cool dance' and if the actor is asked by any other actor through the 'my cool dance' message...it will fire the event and execute the code on that actor for it.
      When you cast and call a function of an actor, you are telling the object this is 'myActorGuyBP' he knows how to do this thing called 'my cool dance' tell him to do it for you...now if you ever decided to delete 'myActorGuyBP' you have to clean up the dependency you created.
      Make sense?
      The biggest problem with casting is that you can accidentally create chains of dependency... If you tell one actor to cast to another actor and then you tell that actor to cast to a different actor class now if you were to load for example the weapon which casted to the player character which casted to the game mode if you were to spawn a weapon in the level it would spawn a default class object of each of the classes that it casts to just to store in memory. Interfaces don't do that. They have the interface object which has a set of functions that you put in it that are empty and you can send a message from one actor to another to execute that function and if you implemented it on the other actor the other actor Will fire that code without creating any kind of dependencies.

    • @rolferikson
      @rolferikson 3 роки тому

      @@omegablast2002 thanks!

  • @crateer
    @crateer 3 роки тому +2

    Absolute Legend! Thank you :)

  • @heliaxx
    @heliaxx 3 роки тому +8

    Just want to say that interface is better in general, if you need to make some big changes cast will make everything WAY more difficulty to change.

    • @RyanLaley
      @RyanLaley  3 роки тому

      Again, as mentioned in the video they are used for two different reasons.

  • @DragLProductions
    @DragLProductions 3 роки тому +1

    Thank you!

  • @djm754
    @djm754 3 роки тому +1

    You mentioned using an interface for online, I'd love to know more about that sort of implementation.

  • @EveryGoodWorkInc
    @EveryGoodWorkInc 3 роки тому +15

    Would love more explanation on when you should use Casting or Interface and the tradeoff in doing so. For example code complexity or CPU price with Casting or what are the downsides using Interfaces. Also some more complex example of interfaces would be wonderful. Thank you.

    • @RyanLaley
      @RyanLaley  3 роки тому +5

      As I mentioned in the video, its less about which is best, it's that they are used for two different purposes. It's like asking which is better a fork or a knife? It depends on the task, use what is right for the situation.

    • @EveryGoodWorkInc
      @EveryGoodWorkInc 3 роки тому +4

      ​@@RyanLaley Thank you Ryan, I didn't think I asked which was best, certainly that was not my intent for the exact reasons you meanioned. But as you've stated, what are the tasks or considerations that we should use to determine what is right for which example situations, and what are the downsides? Would just appreciate some more of your insight. Thank you,

    • @vicwaberub5297
      @vicwaberub5297 3 роки тому +10

      You use an interface for "one-to-all" call, an cast for a "one-to-this" ;-) Its like a teacher in a classroom who asks the class for their ages (interface) or ask Mathilda for her age (casting).
      At third method is the dispatcher...

    • @EveryGoodWorkInc
      @EveryGoodWorkInc 3 роки тому +2

      @@vicwaberub5297 That's a great example, thank you!

    • @KillerSneak
      @KillerSneak 3 роки тому +2

      @@vicwaberub5297 depends on what blueprints/classes are using the interface. If you only use it on one class/blueprint the teacher can still ask for everybody’s age but he would only get mathilda’s answer.

  • @lingster7777
    @lingster7777 3 роки тому

    Hopefully I'm understanding. So casting would be good for something specific and then Using the Blueprint interface would be much better for a pick ups system or damage system to the player. For instance if you have 5 different pickup options with different effects, it would be better to go with the blueprint interface route.
    Thank you for the video btw!

  • @ralphhleihel5079
    @ralphhleihel5079 Рік тому +1

    this channel is a GEM for unreal engine beginners!!! most of the tutorials ive seen tell you what they are doing but don't really explain what those nodes really are !!! tutorials like this one are amazing i wish he could do more of them

  • @FireFox64000000
    @FireFox64000000 5 місяців тому +1

    Sounds like if I want to make a damaged system for my game I probably want to use interfaces.

  • @Karlettto89
    @Karlettto89 Рік тому

    Hi Ryan! Thank you for the tutorial!
    When 2 different classes implement an interface and one of them is loaded into memory, does that mean that all the other classes that implement the interface are also loaded?

  • @rifat.ahammed
    @rifat.ahammed Рік тому

    Thanks

  • @falsehero4476
    @falsehero4476 Рік тому

    Ryan is it ok to have too many(like20 or 30) interface functions and events inside one blueprint does this effect performance? Noob question sorry.

  • @IspyrGameDev
    @IspyrGameDev Рік тому

    Maybe you can help Ryan, but none of these tutorials on this subject shows you how to do with between widgets, like not using "Actors" Would you know the node to use for this? Thx much!

  • @pietpanzerpanzer5335
    @pietpanzerpanzer5335 Рік тому

    If i make a get pawn in the controller i get an acess non trying to read property. How do i cast to pawn for multiplayer properly?

  • @yassiraykhlf5981
    @yassiraykhlf5981 3 роки тому +1

    Interesting

  • @NewMateo
    @NewMateo 2 роки тому +2

    I wish more UE youtubers did more videos on proper design patterns. I am just learning about interfaces after dozens of tutorials and I came across them while reading a book on progamminh designs.
    Its seems really important when it comes to elegent code.

  • @Momijisu
    @Momijisu 3 роки тому +2

    Does that mean it's bad practice to cast to game mode from within an actor to pass information?
    In my current project I was casting to game mode to get a global gm variable - I moved over to using interfaces - but was my initial solution not the right way to go about it?

    • @muhendisbibey
      @muhendisbibey 3 роки тому +2

      There is no wrong or true in coding. The only difference is performance and complexity i think.

    • @SuperNerdEntertainmentShow
      @SuperNerdEntertainmentShow 3 роки тому +2

      If you are going to use a cast it is probably best on the "begin play" or whatever code runs on initialisation to create a reference to the object for later use. (promote to variable) I believe if you do it this way the saved reference doesn't keep using the "cast". In C++ you pass a "reference" in function calls, so you are probably better off thinking in references.
      I cast to the game mode all the time, (using the above method) but I am not making a AAA 50 person online 3D HD 4K experience, so if what I am doing is a performance sink hole, it really isn't going to notice too much on a decent computer.
      9 times out of 10 with an interface you are still going to need to cast or get a reference to something, because the target needs a connection. So you might be using the "get game mode" function node or whatever, but you generally have to pass it something and something specific.
      I don't think any large studios use blueprints for their coding solution, so I am not sure it matters which is "right" in your own home with your own projects. No one need never know how you solved an issue. If you are thinking I need to learn the best practices for getting a job in the game industry, then I would learn a C language.
      So it depends on why you "Care" about being right or wrong in your methods. Most of the time unless you work on something really huge it is not going to matter too much.

    • @TrojanLube69
      @TrojanLube69 3 роки тому +1

      Everytime you cast, you load all within that class and many reference chains being called. Even if store as variable in begin play, there is still storage being used. With interfaces, you get to call just the specific function and better performance.
      There is no right and wrong in programming as mention before. There is just limitations, performance, and readability/abstraction differences.

  • @AlexFord-gp7by
    @AlexFord-gp7by 3 роки тому +1

    Hello ryan I have a question which might seem stupid but I don't know why we cast, like rather than casting to a character why don't we just make a new variable with the type of the character and do the changes we need in a certain events?

    • @RyanLaley
      @RyanLaley  3 роки тому +2

      It's getting that specific reference to begin with that you need cast for.
      Most functions aren't specific, they will return a generic class (makes them more useful) which we then have to cast. Once you have casted it you can save that reference as a variable for later use

    • @AlexFord-gp7by
      @AlexFord-gp7by 3 роки тому

      @@RyanLaley thx alot for helping.

  • @AlexFord-gp7by
    @AlexFord-gp7by 3 роки тому +1

    Can you please tell me the name of the level?

  • @danpaul4108
    @danpaul4108 3 роки тому

    I really like this environment. Can you point me to it in the marketplace? haven't found it yet.

  • @gameriltsforever1093
    @gameriltsforever1093 3 роки тому

    Can you do a tutorial of flying like the griffins in ARK Survival Evolved???
    I know flying is difficult but is the main part of my game
    Thanks if you do!

  • @MonsterJuiced
    @MonsterJuiced Рік тому +2

    This is all still extremely confusing. Main reason is everyone trying to explain interfaces uses the player character at a starting point. What if you need level blueprint to interact with events within actors in the level?

    • @de5niz402
      @de5niz402 12 днів тому +1

      it has 1 years passed but i am writing this for anyone ask this question new. when you collide something, there is a node called does implemented interface. you can get the other actor ref and use it for calling a function. for example i have a interact interface. i am ray tracing in character. when i hit something. does hit actor have implement interface? if so, i can take the other actor ref and call the function of interface

    • @MonsterJuiced
      @MonsterJuiced 12 днів тому

      @@de5niz402 Nice

  • @MrKennykyle1
    @MrKennykyle1 3 роки тому

    How would i cast an actor without using get actor of all classes?

  • @murnoth
    @murnoth 2 роки тому +1

    Once I got past the heavy Bri-ish accent, I realized that your way of explaining this stuff is the Most helpful. Thanks!

  • @musikalniyfanboichik
    @musikalniyfanboichik 2 роки тому

    now please do cast vs class equal (or just ==)

  • @JohnSmith-rn3vl
    @JohnSmith-rn3vl 3 роки тому +2

    I'm a programmer so I know exactly what inheritance is. But I don't see how this works with blueprints it just went over my head sorry ... but I have also forgotten how casting works in blueprints so need a refresher on that (which I will now go and do). This is one of the few examples I have seen you do where at the end I just don't see the point of doing this (probably the first example). So I am off to google more info on it as you wouldn't bother explaining it if it were not useful. If you had explained this with an example of say communicating events between a character and a collision object or another NPC or a volume I think I would have found it easier to understand what is going on here, but this one did not click into place for me.

  • @ThatOpinionIsWrong
    @ThatOpinionIsWrong 3 роки тому +1

    Interfaces are faster, better for optimization if you're often casting to the same thing

    • @RyanLaley
      @RyanLaley  3 роки тому

      Faster but as mentioned in the video are two different reasons to use each case.

    • @RyanLaley
      @RyanLaley  3 роки тому

      Faster but as mentioned in the video are two different reasons to use each case.

    • @jonathanantoine4962
      @jonathanantoine4962 3 роки тому

      Are you sure they are faster? I've remember some tests ran by a guy on the unreal forum and they were similar in perf cost

    • @sebastianvonhohenberg729
      @sebastianvonhohenberg729 3 роки тому

      @@jonathanantoine4962 yeah Interfaces are faster than a cast...this Info you can also found in the blueprint optimization video of epic games

    • @jonathanantoine4962
      @jonathanantoine4962 3 роки тому

      @@sebastianvonhohenberg729 Yeah that's what everyone says but I've seen some experiments ran and it wasn't the case in the data..

  • @crazyguy7585
    @crazyguy7585 3 роки тому +1

    the way u cast then use interface is wrong cast is expensive u can simple use interface to replace cast u do cast then call interface is really bad idea.

    • @PimpPlazaProductions
      @PimpPlazaProductions Рік тому +1

      I was wondering the same thing. My research said interfaces can be used in place of a cast, but in this tutorial he just shows an example that uses both instead of one, seems redundant to me. But ill keep researching!