Stop Casting! use blueprint Interfaces Instead!

  Рет қаралды 6,106

The Game Dev Cave

The Game Dev Cave

5 ай бұрын

You've probably heard that you should avoid casting where you can because it's bad for performance. While I think that is very much overstated and people go WAY out of their way to avoid using any casts, it's still good to know when and how to avoid it, so today let's take a look at a common method, using Blueprint interfaces!
Get the project files here on patreon: / 93566119
or for youtube memhers, here : • Post
Join this channel to get access to perks:
/ @thegamedevcave
Join the discord for any help you need! : / discord
support the channel and development over on patreon : / thegamingcaves

Пікірлер: 74
@vincent78433
@vincent78433 5 ай бұрын
this is so much better then begin component overlap into cast that every other tutorial does
@itsMBWAAA
@itsMBWAAA 16 күн бұрын
You do an excellent job of explaining why/how things work to actually help beginners/intermediates actually learn. I'm sure we all know a lot of tutorials out there are 'do this, connect this, Call this' and we do it and whatever system we are working on works, but we dont learn anything along the way
@thegamedevcave
@thegamedevcave 15 күн бұрын
i'm glad that it's appreciated! :)
@TheHitchboy
@TheHitchboy Ай бұрын
You have finally driven home a much needed part of this for me!!! The Input of your BP_PlayerCharacter in the Interface is such a vital part I was missing. I was trying to send variables through input and output but instead you send the reference to the entire Player blueprint and THEN drag off to get the variables inside. THANK YOU!
@thegamedevcave
@thegamedevcave Ай бұрын
Check out my video on copy vs references too, because i feel that that may be the actual issue gou were running into
@shaishavpathak
@shaishavpathak 5 ай бұрын
Thank you so much for this. This is the type of stuff a lot of people don't make straightforward tutorials for, and yours are quick, to the point, and easy to follow. Plus, you actually explain the stuff (again, in a way that's easy to follow) and help us understand it so we get the importance of it and aren't just mindlessly copying what's on screen the whole time. Fantastic videos. As someone who's making an immersive sim, which means you'll have a TON of different objects to interact with in different ways, this helps so much.
@thegamedevcave
@thegamedevcave 5 ай бұрын
Thanks! i am glad you found it to be helpful :) I put a lot of effort into making my video's a tool for learning rather than just copy-pasting like many tutorials are so it's nice to have that be apricated!
@KaitheArtGuy
@KaitheArtGuy 5 ай бұрын
This is a great explanation! Lot's of tutorials use BPI without explaining what it is exactly, this helps everything make sense! Subbed
@thegamedevcave
@thegamedevcave 5 ай бұрын
Glad it was helpful!
@ShaddieCakes
@ShaddieCakes 5 ай бұрын
This is actually really incredible. I knew about interfaces but when using them I was always just a little confused about them since tutorials don't go in depth about how they work. Now I can hopefully get some more of my ideas working better with better performance.
@thegamedevcave
@thegamedevcave 5 ай бұрын
yeah it's tricky to wrap your head around, especially since casting is so much quicker and more straight forward, so most tutorials use that instead. While I think a lot of people go over the top to try and avoid casting, it's good to know your options!
@hamidrezaoloumi
@hamidrezaoloumi 5 ай бұрын
thank you for your explanation about the blueprint interface and casting, It is the first time I understand how the interfaces work. I always had problems with casting, specifically Objects reference for casting.
@thegamedevcave
@thegamedevcave 5 ай бұрын
it's still worth while understanding how casting works and when you should use it. but for something like the example in this video, interfaces work a lot better :)
@dragondev2617
@dragondev2617 Ай бұрын
this video is so underrated, It's straightforward and easy to understand and memories, thank you so much!!
@thegamedevcave
@thegamedevcave Ай бұрын
Glad it was helpful!
@NervCorp
@NervCorp 4 ай бұрын
I absolutely love these videos and I can't get enough. Thank you so much. All these more complex (for beginners) topics I don't see talked ever (unless they're in a 40-video-long playlist) made so simple to make independent game development that much easier. Hats off to you man :)
@NervCorp
@NervCorp 4 ай бұрын
it's also 4 am and i've been binging these videos. just found your channel an hour ago
@thegamedevcave
@thegamedevcave 4 ай бұрын
Glad you like them!
@johnrex7108
@johnrex7108 2 ай бұрын
Very helpful tutorial. Appreciate you taking the time to make it.
@thegamedevcave
@thegamedevcave 2 ай бұрын
Glad it was helpful!
@siete-g4971
@siete-g4971 4 ай бұрын
thank you, amazing explanation
@sciencegame645
@sciencegame645 5 ай бұрын
I'm usually just a lurker, but this is a great video. Keep it up!
@thegamedevcave
@thegamedevcave 5 ай бұрын
Glad you enjoyed it!
@sciencegame645
@sciencegame645 4 ай бұрын
@@thegamedevcave I already knew the contents of this video, but I hope my engagement helps your channel!
@Restart-Gaming
@Restart-Gaming 4 ай бұрын
WOW keep up the great work still new to unreal love to learn
@chankulovski
@chankulovski 12 күн бұрын
When you create the Input as an Object Reference in the Blueprint Interface, it creates Hard Reference and it's the same like the Casting, it's still loading the Character BP the same like the Casting.
@thegamedevcave
@thegamedevcave 12 күн бұрын
Yup
@alexanderskanderup6791
@alexanderskanderup6791 3 ай бұрын
I'm very new to game dev/programming/UE, and still wet behind the ears. Is there a "functional distance" on the line trace ? Or would you be able to interact fra across the map?
@thegamedevcave
@thegamedevcave 3 ай бұрын
with a basic interaction ssytem like this I use a line tracer. for a lien trace you give it a start and end point, so it's limited in length. if you want to make that length uper longer, or super short, you can :)
@alexanderskanderup6791
@alexanderskanderup6791 3 ай бұрын
@@thegamedevcave thanks man !
@Penthast
@Penthast 4 ай бұрын
Good stuff
@thegamedevcave
@thegamedevcave 4 ай бұрын
Glad you enjoyed
@RV-bc9yi
@RV-bc9yi 4 ай бұрын
Hello, thank you for your vidéo. Time 05:20, I don't have the "Coin" variable. Not found with UE 5.2, 5.3 even without context sensitive...nothing! In my BPI, I'm in First Person Character Object reference. Do you have an idea?
@thegamedevcave
@thegamedevcave 4 ай бұрын
you gotta add that to the player class first. I'd recommend taking a look at my unreal basics course playlist
@RV-bc9yi
@RV-bc9yi 4 ай бұрын
@@thegamedevcave Ha, There are a lot of videos! Could you tell me which one please?
@RV-bc9yi
@RV-bc9yi 4 ай бұрын
Unreal Engine 101 Basics Course ?
@thegamedevcave
@thegamedevcave 4 ай бұрын
@@RV-bc9yi kzbin.info/aero/PLoReGgpfex3x295NSo3hZD6Ylq6hPqdz8
@RV-bc9yi
@RV-bc9yi 4 ай бұрын
@@thegamedevcave Thank you so much for this video the BPI is clearer for me now !
@cleandust530
@cleandust530 3 ай бұрын
Sometimes, casting is more useful than calling a BPI. This is because you cannot know in advance whether a particular actor has implemented the required interface.
@thegamedevcave
@thegamedevcave 3 ай бұрын
if you're casting to a class you know what class it is you're casting to, and as such you will also know if that class implements an interface. but with interfaces you aren't limited to 1 class (+children). So while casting has it's uses, very often interfaces will be better to use. if you're worried about running something on a class that doesn't have the BPI implented, trying to message in that case simply won't do anything, so that's not really a problem. if for whatever reason you need to know if the actor you just tried to run your BPI function on does have it implemented or not, you can just add a bool on return , if that's false you tried to run it on something that didn't have an implementation for it. Now if you need a very specific value or execute a very specific function that only that 1 class has, of course there's very little use in making a whole interface for just that, in those cases just doing a cast is more straight forward. As well as in cases where you know a class is going to interact a lot with another class. (your player character having a cast to it's weapon for instance), there it also makes sense. You certainly dont have to replace every cast in your project with an interface, but largely, a lot of people could do with casting a bit less and using some interfaces to keep their memory footprint under control.
@peterthe_geek
@peterthe_geek 4 ай бұрын
How well does this work with child blueprints using inherited functions? I'm excited to start implementing this, but right now I use a lot of Child Blueprints off of one root interactable BP
@thegamedevcave
@thegamedevcave 4 ай бұрын
I believe if you implement the interface in a parent class all the child blueprints may also have access to it, not 100% sure though but using interfaces makes the need for a parent “interactively object” unneeded anyway
@peterthe_geek
@peterthe_geek 4 ай бұрын
​@@thegamedevcave thank you for the reply! I'm just so used to using inheritance in coding to make sure I don't have to re-type anything, I was curious to see how the two concepts interacted in Blueprints. I'm excited to start playing around with interfaces! This is the first video I saw that actually explained them in a way that clicked for me. Thank you!
@mosesgvr1456
@mosesgvr1456 3 ай бұрын
It drives me crazy not seeing you compile or save anything as you do these lol I have a question, is it more performant would you say to use a line trace on the characters feet to allow for an automatic interaction via interface, like stepping on a button, as opposed to using a box collision with a cast?
@thegamedevcave
@thegamedevcave 3 ай бұрын
yeah, used to working in c++ where saving is less of a priority because when unreal decides to crash it doesn't delete all your progress XD as for compiling, there's no real reason to do that, when you run the game in editor it auto compiles all your blueprints first anyway so pressing compile and then running the game is a waste of effort XD (now if you need to add variables to a blueprint that you need to get with a cast or whatever in another blueprint, compiling does matter of course) as for your question, if you're making a button to stand on you probably want to go with the route of putting a box collision on the button (overlap) and check if the actor that overlaps is == to getPlayerCharacter. the player itself has no need to have any code regarding a button like that so you dont need to do that with line tracers and an interaction system like the one in this video.
@mosesgvr1456
@mosesgvr1456 3 ай бұрын
@@thegamedevcave Ah ofcourse good point, so just creating an object reference to my pawn and checking if it's overlapping. No need for casting or line traces there then. My main priority at this time in my progress is figuring out ways of doing everything in the most simple and modular way I can come up with. Thank you :)
@thegamedevcave
@thegamedevcave 3 ай бұрын
@@mosesgvr1456 yeah the overlap event for your button will come with a object reference to whatever actor triggers it (so that will be your player if the player steps on it, an enemy if it's an eney that steps on it, etc) you can compare it to the reference you get from GetPlayerCharacter without having to cast or anything so you dont need to make any variables that hold the reference to the player, the overlap event and GetPlayerCharacter nodes will provide those for you
@mosesgvr1456
@mosesgvr1456 3 ай бұрын
@@thegamedevcave This is great, so what I did was from the 'other actor' pin in 'begin overlap' I created an == and pulled out from that 'getplayerpawn'. Then from the bool pin I put that into a branch and it works perfectly. What's odd is that nearly every tutorial or instruction I come across always always uses cast from overlaps. Maybe it's necessary because I don't know all that much yet, but I knew there might be a way without casting to achieve the same result.
@thegamedevcave
@thegamedevcave 3 ай бұрын
@@mosesgvr1456 casting is only needed if you want to access information or events on your player that don’t exist on a generic actor. If you just want to compare them you don’t need to cast. I have a video that talks a bit more about casting in my unreal basics playlist that might be worth checking out, it’s a tricky concept to wrap your head around haha
@cliffordgardner7975
@cliffordgardner7975 9 күн бұрын
I keep getting a "Accessed None trying to read property K2Node_Event_[Interface Input Name]" everytime I try to get a bool variable from my player blueprint even though I set it and when I use print string I get the character value but the moment I put my bool variable i get from my interface into a branch I get this error PLEASE HELP
@thegamedevcave
@thegamedevcave 9 күн бұрын
hard to say without seeing your node setup but that sounds like your character may not have the interface implemented? usually an error like that implies an actor variable not being assigned a value though.
@akzork
@akzork 4 ай бұрын
Made me sub.
@thegamedevcave
@thegamedevcave 4 ай бұрын
welcome! :)
@WesleyOverdijk
@WesleyOverdijk 2 ай бұрын
I don't understand why this works. Does any interface's functions just always get added to all actor types as messages? Because in code, you'll still have to check if an interface is implemented or not, so blueprints just showing all messages confuses me.
@thegamedevcave
@thegamedevcave 2 ай бұрын
blueprint just does that for you, when you try to messege, it checks if the interface is on that actor's class, if it it, it'll run the implementation, otherwise it'll just move on
@WesleyOverdijk
@WesleyOverdijk 2 ай бұрын
@@thegamedevcave Dang. That's pretty cool. I'm refactoring my pickups as we speak. Dankjewel man
@Ben_of_Milam_Music
@Ben_of_Milam_Music 4 ай бұрын
random aside, but my personal solution for a system where the player is able to interact with a great deal of objects was to create a parent object and name it something like "BP_Interactable," and put some empty "On Interacted Events" and just cast whatever we hit into an Interactable and call On Interacted. ofc every interactable object you make must be a child of BP_Interactable. I'm too high to remember how to do it, but just do a blueprint function override on the On Interacted function and that way you can have your child objects be things totally different things like doors that swing open, boxes that break, priceless art which can be defaced, ect. It was a good enough solution and I wanted to share lol
@thegamedevcave
@thegamedevcave 4 ай бұрын
that's a decent workaround but generally, using an interface does the same thing and avoids casting altogether and is more flexible. But effectively this workaround works more or less the same way! (just annoying whenever you're deep into a project and you suddenly need to have a actor that's a child to a different class but should ALSO be interactable, unless literally every actor class you make is interactable but then you run into issues with, what if i want to make a pawn that can be interactable or a character? do you make your interactable actor class from the character class? because then you have the issue that everything in your game is now a character without it needing to be, etc.)
@Ben_of_Milam_Music
@Ben_of_Milam_Music 4 ай бұрын
@@thegamedevcave yup! it works fine for something like a single player puzzle game with no NPCs or AI, but isn't flexible enough for a more complex project.
@aff3nm4nn
@aff3nm4nn Ай бұрын
@@thegamedevcave Your videos are nice to watch, but damn, nobody would ever do it like that. That's simply not how you would use interfaces even in small scale productions. Inheritance, dispatchers, interfaces all go hand in hand. There are always different approaches, but the one you describe here is impractical and ends up being a total mess once you step over a certain threshold of scale in production. The way youtubers teach topics like this is horrendous.
@thegamedevcave
@thegamedevcave Ай бұрын
@@aff3nm4nn of course there is no single "catch all" method. in some cases interfaces make more sense, in some cases inheritance makes more sense. and sometimes you dont want to use either and you're better off using delegates. The issue with youtube tutorials is that they dont have production context, so just showing an example without any surrounding context is limited in how much you can truly show each usecase. All I can do is to show the options, explain what they do and what the upsides are and leave viewers to make their own decisions on what fits for them. Anyone watching tutorials and just blindly copying things over 1 to 1 isn't actually learning anything anyway, you always have to apply things to your own projects with all the context of that code you already have in that. as for your comment "nobody would ever do it like that", i assume this is referring to the example in the video showing the basic interaction system? Because I think that this is a pretty good way to do things. You could use a parent "interactableObject" class instead if you wanted to but that could very much lead to limiting yourself down the line. Again, this is very context dependent, if you know that all the objects you will every interact with can neatly fit into your actor hierarchy, it might be easier and more straight forward to do that. But there isn't any "right" or "wrong way about this without a lot of surrounding context. If you'd like to give some more details on your opinion I'm all ears though.
@neonghost1417
@neonghost1417 2 ай бұрын
💯💯💯
@Defenceless_baby
@Defenceless_baby Ай бұрын
This still does create a hard referance to player tho right?
@thegamedevcave
@thegamedevcave Ай бұрын
no, interfaces are kind of like event dispatchers, they just call out and if the object you're messaging it to has an implementation, it will run that code. the caller doesn't have to be aware of any classes it communicates to. of course, any parameters you may put into an interface will still create hard references.
@Defenceless_baby
@Defenceless_baby Ай бұрын
@@thegamedevcave i hope that is the case. but looking at reference viewer it has created a hard ref. it doesn't if there is no input for player character in the interface but creates one if there is one. (like you did to get coin)
@thegamedevcave
@thegamedevcave Ай бұрын
i have to assume you have another hard reference between whatever classes you're working with because the interface's whole purpose is to avoid that. both classes have references to only the interface but not each other.
@Defenceless_baby
@Defenceless_baby Ай бұрын
@@thegamedevcave i'm stomped. the BP has literally nothing but the Event from interface. with nothing attached to It either. Obvielsy event has the ref just like you created one around 4.20
@Defenceless_baby
@Defenceless_baby Ай бұрын
do you have community discord by any chance?
@thebestchillsounds4780
@thebestchillsounds4780 Ай бұрын
I can't stand most tutorials out there. Everybody using casting, are they not aware it's terrible due to huge memory allocation?
@thegamedevcave
@thegamedevcave Ай бұрын
Most of the time, tutorials showing off something entirely unrelated don't want to bother setting up an interface because it takes more time, adds more possible confusion for a viewer and just adds a lot of stuff that is faster to solve with a cast. Where the issue comes in then is that people watching only 'how to make X feature" tutorials think that casting is the only option to communicate between objects of different classes, or even if they are aware of other methods see castings as the "default" way to do it because they just copy what they see in tutorials without thinking about what everything does. In a lot of tutorials i will also throw in casts for those reasons but I also put in a lot of effort to make videos like this to make sure people have a recourse to learn more about how things work beyond just copy pasting nodes that I place. At some point, if people don't want to learn beyond a surface level though, that's on them. btw, i also wouldn't call casting "bad" just misused and overused. the real danger in casting is, if you do it everywhere you create a cascading effect of dependencies, that is where it becomes and issue but there is plenty of scenario's where you just cast to a class which then itself doesn't do any more casts and that's fine really but those are things you have to be aware about, which thanks to a lot of tutorials indeed, a lot of people are not.
SOFT Object References in Unreal Engine EXPLAINED
11:58
The Game Dev Cave
Рет қаралды 7 М.
How to ACTUALLY Load Levels in Unreal and Make Loading Screens
16:31
The Game Dev Cave
Рет қаралды 27 М.
Teenagers Show Kindness by Repairing Grandmother's Old Fence #shorts
00:37
Fabiosa Best Lifehacks
Рет қаралды 43 МЛН
Joven bailarín noquea a ladrón de un golpe #nmas #shorts
00:17
USE Gameplay Tags
10:38
The Game Dev Cave
Рет қаралды 34 М.
I Struggled With Blueprint Interfaces for Years!! (Unreal Engine 5)
16:48
Glass Hand Studios
Рет қаралды 167 М.
UE5 Understanding Delay - Be a better game dev
13:29
LeafBranchGames
Рет қаралды 6 М.
UE5 Understanding hard and soft references - Be a better game dev
19:58
How to improve your debugging skills - Be a better game dev
16:56
LeafBranchGames
Рет қаралды 3,7 М.
I solved Unreal Engine's Package Size Problem...
14:35
Cobra Code
Рет қаралды 49 М.
Why Dependencies are Bad and How To Avoid Them In Unreal Engine | UE5
26:40
How To PLAN your Game as a Solo Developer
16:26
Heartbeast
Рет қаралды 442 М.
Airpods’un Gizli Özelliği mi var?
0:14
Safak Novruz
Рет қаралды 7 МЛН
Nokia 3310 versus Red Hot Ball
0:37
PressTube
Рет қаралды 2,3 МЛН
⌨️ Сколько всего у меня клавиатур? #обзор
0:41
Гранатка — про VR и девайсы
Рет қаралды 563 М.
Чем отличается OLED от AMOLED?
0:43
Не шарю!
Рет қаралды 601 М.
IPad Pro fix screen
1:01
Tamar DB (mt)
Рет қаралды 4,4 МЛН