Unreal Engine tutorials dont teach you about this...

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this tutorial is sponsored by patreon thank you all for your support hello everyone welcome to the new video in today's video I wanted to create this video because I saw so many tutorials on YouTube uh not showing a proper way of doing stuff uh basically they would show you how to do something but in reality you cannot even build up on it it's all bad practices uh like ticks bindings uh cast everything so in today's video I'm going to show you how to be efficient in Unreal Engine 5 and how to write a clean code even as a beginner so let's start the first thing that we're going to talk about today is UI okay so you know when you create UI right now we are in uh my third person template but when we create our UI user interface with blueprint let's name it Master UI right now we can open it up and let's just do basic setup let's add the canas and let's add some basic text to the canvas I'll put to be red for this example and now uh I'm just basically going to uh print this uh to my screen uh usually I would do this in player controller uh it's much more efficient but here I'm just going to do it here because I'm just just showing you out uh I just want to show it on the screen so I'm going to add it to the viewport like that so now if I click play I'm going to see the text block so let's say our uh player for example has a currency let's say he has a money that money would probably be of type float in your game so let's put it as a float so we have this currency here so right now there is a couple ways how we could do this we can create binding or we can do it the proper way so first I'm going to do it the wrong way and then I'm going to tell you why it's wrong so let's go ahead and click create binding so what this binding does uh it requires us to put something inside so it gets return value in our case we want to return money but here's the thing first to have this working we we need to cast to the character or we will use interfaces which we are going to talk about later I'm going to be very slow with this one so uh what this do is it's going to run every second every tick so even more times per second sorry for that I'm kind of sick okay we can continue okay so you would see why we don't want that we don't want to run the currency text 20 times let's put this to Green just for my OCD because it's money so you see why it wouldn't be good and performant to run this code every second because we want to run this code only when we want to do something with it for example we want to spend money we want to receive money etc etc so now we're going to go to the um here to the go to function and let's demonstrate how would you uh create this at first so what you would do is first you would need to cast where your variable is so BP thir person character in my case uh get player character this is the wrong way I'm just showing you and then you would get the reference for the character and then uh from here you would just get the money uh and this would work of course so now if I create some uh random uh event here that says every time I press five I get um don't know like 50 money like that so now if I go ahead andame game as you can see it already updated so it works if I press five it adds money instantly so it works is it performant no so what happens here is while you are doing whatever you are running around the cubes your money is getting updated on every tick even though we are not using it and we don't need it so you see why this is bad so let's do it a good way so let's delete this let's delete this binding as well like that and now to be able to access this text in your graph you need to give it a name first let's say money undor text and very important set it as a variable because if you don't you're not going to see it in your graph so if I compile now I'm going to sit here also I want to delete this because we are not going to use that okay so right now what we need to do here is we need to create custom event and it's going to be called update Ur this is how I like to do it so I have one master Uh custom event and then I have for example uh a lot of small ones like update update money uh also I'm going to add underscore here so what I do with this is I will pull update money here and uh I I will just pull this update UI so now because we're not binding and it's not running every second we need to do it by the hand yes it's a lot more work but you get the performance back and we should say performance where we can so this update UI can be called basically when we construct this so this will make sure when we load the game your money is going to be updated so if you have 50 here once we load the game uh it will update but uh of course uh this doesn't do anything right now because we didn't set up anything so as you can see now it doesn't update and it just says textbook so we can update this by the hand so we can get our money text that's our variable right here is text variable and we can just pull set text but now we need money variable and a lot of you right now would just go ahead and you will just cast here because if you're a beginner this is what they teach you and I don't blame you uh what you want to do is use interface I want to go slow so you understand but interfaces blueprint blueprint interfaces are used so you can communicate between blueprints so for example right now if I go ahead and I create my blueprint interface and I can call it BPI tutoral you can call it whatever you like you can have what like however you want this interfaces you can use it wherever you like in my case 95% of um stuff that I make and program in blueprints I can use blueprint interfaces I barely use custs same goes for atics atics is good when you are working with animations basically but other than that you're not going to use it so much okay we have function here but here we cannot do anything but what we can do here is we can call this get current money so right now this does nothing this is a uh if we pull it here inside of our BP person character we have to implement that so let's Implement BPI tutorial if I press compile under interfaces I'm going to have custom event that I can call anywhere but for my case right now where I want want to get something so I want to get the information I don't want to do anything I just want to get the information I'm going to do it different way so events only have inputs but functions can have outputs so right now I'm going to create output here in my get current money and I'm going to call it money amount it will be type of float because my other variable is float so right now if I press compile you're going to see this uh function became gray well because it's not event anymore so it canot be yellow so it's a function as you can see just like your normal function that you would make in your under engine but it has return note that asks okay give me the value of money so I will pull this money and now he has the value so can you guess how would I now connect this here well because we did use interfaces we don't need to cast we only need to uh call the function and give it a Target that's going to be where it's stored in our case bp3 person character so let me demonstrate from here I'm going to call get current money and you're going to see in the brackets it says message that means it's uh communicating between Blueprints and the target should be where you use interface so for me I use it in third person character so I will get player character as you can see you have get player character get player controller get player Pawn State everything for me I'm going to use get player character so now what I can do for example I can format this text curly brackets money so I can edit it here so I can pull this to be my value and let me add just some dollar sign in front and this will update my money let's see how would that work as you can see it works but if I press five to add $50 doesn't do anything but it did update on begin play well that's because we only called it on the begin play so now this update money usually you can you could call it from the player controller because you would have the reference to your master UI in the player controller so you could call this event but now we do not have that so what we can also do is this also cannot can be interface so let's go ahead and create another blueprint interface let's call this one BPI Master UI and let's call this one update money we don't need input we don't need output this uh this time so all that we need to do now is we need to implement this interface here so we search for master UI and again we have the yellow interface uh which means it's event and now I can simply do this I can do that or I can just simply connect it to here for now to make it even simpler for you I'm going to do it this way so now I will create function here when I give money so let's call it add money this is simple function we can even copy this here and let's just uh add money connect to here so let's copy it here like that but what we also need is we need update money message also we need a Target to this so either we would get the player controller reference and um Master UI reference from there we could also because we made it in the character but usually it should be in the player controller we can also use it uh to promote the variable so now we have Master UI variable here delete that let's call it Mastery reference uh I'm getting some errors oh yeah so this requires Target so right now where did we put it we did put it in the master UI right so it's either I would do it this way to get the reference or if I or if I was in another um blueprint where I don't have the reference I would just get widget of class and it would be my master UI found widgets would be this so what should it do now when we add money we should receive $50 and it should update it as you can see here when we press five we do run this function so if I go ahead and press five it's going to update and it's not ticking and it's only running when we need it okay so now we resolved two problems we resolved binding problems where the variable is sticking in our text and now we are updating manually but we also stopped ourself from casting also if you delete this and just get the basic UI reference that you already have you could also access this by your uh player controller cast because basically you would have the cast here because of the enhanced input local player subsystem you could uh get this master UI reference from your player controller but it's important you always when you're are programming you always get the variables so it's not hardcoded so also here what I can do is I can simply pull this here call it amount and now if I'm going to add the money I can just simply say okay give me 15 and now we even try with this instead of the get all widgets and you will see it's going to work now we are getting 15 but it works okay so this works but I want to show you one last thing between like you and um uh actors in the level for example um let's go ahead in BPI tutorial let's get a new function and call it add money or uh increase money because we already have add money so why I'm doing this now is because I want uh to be able to contact with other blueprints about receiving money so this is going to be deleted right now so in output oh in input sorry I just need M amount and now I can go here and I can call increase money and if I want to do it this way I can just simply I can just simply copy this here right but let's not do it this way let's just uh delete this stuff here copy it here we can delete add money so what we are doing here now we are making this accessible from every blueprint so right now um I can set this here and this going to work the same way you'll see in a second so this does uh do what we need it will increase the money plus it will update our UI so very good practice but we need something to interact with let's get something for example um blueprint class actor let's call it BP Cube this will be just simple Cube that's going to stay there we can give it a color and when we uh overlap with her box Collision what is going to do is simply it's going to uh disappear and give us something like um $50 for example so let's see how would we do this so as per usual if you have a box collision and you want something to overlap Unreal Engine made it really uh simple add the end on component begin overlap what we want to do is we want to increase money give it a 50 and after the after that I want to just just destroy actor so now we are contacting our third person character so we need our get player character this is not casting so we just get it uh get our Target so the code knows from where it should look at so this will work let's just add simple print string um money received yay and let's give it a green so right now if I go ahead and put this actor in My Level it's kind of big but if I go and play the moment I collide with his Collision it should say um receive money yay and I should get uh if I believe $50 and it should destroy itself and as you can see I'm getting that so we are successfully uh communicating between those blueprints without casting so you can imagine what else you can do with this how powerful this is so that will be all for today and in the next series on my YouTube I'm going to try to um cover these topics that are not covered in other tutorials uh so if I did help you please leave a like and comment uh let me know if it helped uh it will help me greatly with the motivation to continue doing this thank you all for watching until the end I hope I helped you and see you in the next video
Info
Channel: RubaDev
Views: 17,377
Rating: undefined out of 5
Keywords: #UnrealEngineTutorial, #gamedev, #IndieGameDev, #fyp, #UnrealEngine5Tutorial, #ue5, #MasteringUnrealEngine5, #GameDesign, #UE5Tutorial, unreal engine 5, unreal engine 5 tutorial, unreal engine 5 tutorials, unreal engine tutorials, unreal engine interfaces, unreal engine blueprint interfaces, unreal engine blueprint interface, blueprint interfaces ue5, ue5 blueprints, unreal engine 5 blueprints
Id: 0rcuk66MPFM
Channel Id: undefined
Length: 19min 55sec (1195 seconds)
Published: Wed Apr 03 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.