UE5 Understanding Delay - Be a better game dev

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome to another episode of be a better Game Dev today we are going to be talking about delay nodes we will go through what they are how they work why they are bad why they are terrible and also why they are not good now just kidding there are some cases where delay nodes make sense but they are very rare So delay nodes can cause a lot of problems so what we will be doing is we will be looking at some bad use cases some solutions to better approach these use cases and also in the end mention some reasonable situations where theow NES the the delay nodes can be useful so let's check it out okay so let's talk about the delay node what is the delay node well the delay node is a node that you can call that has an input of an amount of seconds that you want the delay to happen after the delay has finished it will continue to execute whatever code it has to the right of it so this is something that stalls your code and refuses to execute anything else until the delay has passed you can either do it in this way where you uh assign an amount of seconds or you can call on it with the duration of zero seconds which will uh be interpreted in the engine as it will wait one frame U one tick generally um before it does the execution and there are some some cases where where this is useful now I I want to point out um delay nodes exist in almost all programming languages in some form where you just wait a period of time and my personal opinion is that you should almost never use them preferably you should never use them but there are some circumstances where it might be useful now I'm not saying that it's stupid of unreal epic to have these nodes like delay and tick and such in in the engine all of these nodes make sense and they have a purpose for existing so it's not not like it's bad of them to have these in the point I'm trying to make and to get across here is that if you use these tools wrongly it will have dire consequences so you need to be aware of how to use them and what it entails so that you can make use of them better and preferably as little as possible right anyway the delay node in my opinion is what I call a shortterm solution that gives long-term problems because it's very common that uh user don't know what they're doing and they're like ah something is going wrong here let's put a delay node here and maybe that'll fix it and it fixes it but they don't know why and then later down the road they will have problems and they don't know why those problems are occurring it it's going to be difficult to troubleshoot and such so that is why it is in my opinion good to have as few or none of the delay nodes as possible but now that we have talked about what basically is the delay node let's look at some use cases of what you should not do and what you could do as an Alternatives and such things so in the case here I have a bit of code I have a character in this world where I can left click to uh play some animations now this code is not important that it's this happens to be about animations but it could be basically anything that the same logic applies here which is that you have some code running or you want to have some code running then you have a delay node that is stalling the code before you do some other piece of code in this specific example I have on my left Mouse button that I disable the player's movement then I play an anim animation because I don't want the character to move while it's doing its animation let's say then I have a delay that is going to be at the duration of this animation before it allows to continue and after that it has happened it enables the movement for the player again now this all makes sense right it seems sensible of approach so to display what this looks like if I have my character here I run I attack it stops and then when it's done with animation it continues running that all makes sense right the problem with this is that and delays like this is that it creates an unresponsiveness um in this specific instance where we have animations we might want to have it being canceled for some reason maybe our character gets damaged and that should interrupt the animation maybe some other cause could cause the animation to stop right and to simulate this I'll put a right click here that will allow us to interrupt the animation and that's the only thing that it will do so also you can see that I have some text here that's essentially giving us some feedback about what is happening so now I click the animation Interruption button and then it runs that code so if I run my character now and I press my attack you can see it disables the player and then enables the player but what happens if I start attacking and then interrupt it immediately well you can see the animation stops but the character is still disabled this creates a very unresponsive feel for the character right because it will never give me back my control of my movement until the delay has finished so what is a better approach to using delays in these kind of circumstances well if we take this left M Mouse button event and move over here we have something very similar over here over here we're disabling the movement again we're playing an animation just like before but instead of using a delay this is my preferred use for delays in every case essentially always use timers instead of delays because I feel delays have almost no uh existence where it's actually useful except for a few which we'll talk about little bit in the end but essentially it's running a timer here by an event uh for the same duration meaning that at the point where the the time has passed here this event will call and we can say okay give back our movement to our character to be able to say that this event should stop prematurely we also need to have a uh reference to the timer itself so we can cancel it once this event has been running and in our Interruption over here when we click on right Mouse button instead of just stopping the animation we also want to say we want to call on our event over here so that this event can be called before the full duration of of the time has passed so let's take a look at what this looks like so if I go in here I right click and then I run I left click for attack and then interrupt and immediately you see that we stop the animation and we also get the ability back to move now this creates a much more responsive feel for the character and is in my opinion a much better alternative for this specific case I hope that all of that made sense let's move on to another example so another example I see sometimes is you have code like onti and then you have something like a sequence and then you have some code here in this case we're running some Trace walls then we have a delay and then after that we're onun a roof trace and after that we calculate some movement Blends and stuff like that so this in my opinion is a bad approach because you have a tick which is supposed to happen in most cases on every frame that's being rendered in the game this is completely opposite to how a delay node works because a delay node is something that's set for a fixed time and then runs on code while event tick can be variable depending on how your computer is performing currently in whatever it's doing so this can go up and down a little bit so having a delay node intake is a really bad idea so when it comes to for solution I say just write code that doesn't use the delay node at all when it comes to tick if you are in the need of using tick for something like this so the code here itself isn't really relevant and and not really important it's just that you you should probably never have a delay node inside of a take event okay moving on my next example here is of something that happens usually uh when you have initializations working but it can be something that is happening for other reasons as well but that is essentially it's in this case we have a begin play for a character where we set up some uh things in this case it's not really important it's setting up the HUD so I can show the just for your on screen and such things and after that we might want to run some code so in this case we have a blueprint component an actor component here that's over here simulate Grid it's supposed to simulate running some kind of initializations when it's created the component itself and then we want to somehow interact with whatever data or or things that the component has available to it in this case the Run main functionality is this event or function that simulates this so if we were to uh run this code in the the game we can see component not ready run failed so a very natural way to approach this would be like okay well it needs some time to initialize we'll just put a delay node before it and then we'll have it run and that should fix it right so if we run again we can see that okay yeah uh it's delaying and then you can see that okay run successful it it had time to do whatever the component in this case needed to do so problem solved right no problem not solved what happens if you end up in a situation where this component needs to operate on larger amounts of data or it needs to do something that takes more time this is a fixed value for the most part you you can have it variable but you will not be able to flexibly determine how long this operation will take so if we were to have 2 seconds now and call on our heavy simulation actor component instead you can see that 2 seconds was not enough so this could be something like uh maybe we had the when we have a small level it takes a certain amount of time to load whatever it needs and then we have a really large level and it takes a lot of time to read and and then the delay will not be enough and essentially we have a broken nothing is working what like we're intending right so the approach to fixing something like this is to avoid the delay again if we go to the right here can see here that I have a a similar example where I have a character set up and then we want to run the main functionality here right and we want to run it for the heavy simulate grid here but in this case what we want to do instead is we want to bind an event we want to make this event driven so what we say is we want to have an event we want to bind to in this component and say whenever that is done we want to run this code over here in this event so what does this mean well in our simulation component here it just has a timer to simulate whatever time is going to be taking for it to do whatever it needs to do and once it's done with that it says it's ready to run and it calls an event dispatcher this is the event dispatcher that we're actually binding to here so we're saying here that on our begin Play We want to call our component and say hey when you're done with your stuff please let us know and then we'll actually run the code on you that we want to run uh this example is is a little bit simplistic and and and reality will probably be more complex but hopefully understand the um the idea behind this because now if we run here it doesn't really matter how long time this actor takes to a component takes to run it will let us know when it's done and then we execute code on it right so this avoids the delay and also lets us run the code when it is ready okay so now we have seen three different examples of what not to do and what you could do instead what are the cases when you could actually make use of the delay nodes that are reasonable well in my opinion something like the delay node is something that's useful for something like let's say you have a screen that you wants to appear it maybe shows some animation or something like that and you want it to be not skippable you want it to take its course and let's say that is two seconds you have something very specific that's going to happen it's always going to be taking that amount of time you can run a delay node for something like that that makes sense the delay zero is something that runs operations and and causes you to delay one frame and this can be useful because some uh things in the engine depending on what making use of have data that is only available first the next frame has been T calculations and stuff like that possibly because of tick groups and such things so the delay node for zero is something that's in my opinion probably more common and useful than this should probably be which is probably for very hard-coded specific and rare situations anyway that's going to be all about delay nodes for today keep on learning take care a big thank you to all of you who like comment subscribe and share my videos or through other means support this channel you are what makes this channel grow and become a resource for other people to learn from
Info
Channel: LeafBranchGames
Views: 5,928
Rating: undefined out of 5
Keywords: learning hub, unreal engine, tutorial, unreal engine 5, blueprints, game design, game dev, unreal engine 5 tutorial, unreal engine 5 beginner tutorial, ue5 beginner tutorial, building user interfaces for unreal, engine ui design, user interface, design ui, design tutorial, building user interfaces, unreal engine user interface, Leafbranchgames, Good habits, Best practice, How to build menus and UI for Unreal engine 5, the right way, UE4, UE5, Reuse, Recycle, be a better game dev
Id: YVW74JSRhuA
Channel Id: undefined
Length: 13min 28sec (808 seconds)
Published: Tue Dec 26 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.