Introduction to Game Development (E21: coroutines)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello in this episode we're going to look at Koreans so as you know if we have some method containing a bunch of code and then we call that method all that code has to finish running before anything else can happen thus if we were to create an infinite loop inside the method by simply writing wild crew then the game would obviously freeze up as soon as the method is called because the method would never finish running let's not turn this method into a Korean first we change its return type to iron numerator and then when we call it we pass it into a method called starch Kuragin doing this allows us to use a special yield statement inside of the method or rather Co routine as we now refer to it one example of this would be yield return new waitforseconds into which we can pass a value for the number of seconds and say point 5 now each iteration of the loop will be separated by half a second during that time the rest of the game's code is free to run and so despite this infinite loop the game won't freeze it's important to note that curry teams are a part of the unity engine and so the functionality is not available in c-sharp projects outside of unity the starter routine method in particular is part of the mono behavior class so co-routines can only be started from a script that inherits from honor behavior if you're wondering why the coach in returns this thing called an iron numerator that's just to do with how co-routines work behind the scenes which i won't be talking about in this video alright let's jump into unity to experiment with code in the mesh so I'm going to start by creating a c-sharp script called test and also courage a little cube object to attach that script to and then we can open it up now this starts with a simple example the requested KO routine that takes in an array of strings called messages and also a float called delay and it's just going to print out those messages one by one with a delay between each of them so we've already seen how to create a coercion we need a return type of ienumerator and what the collisions name such as call this print messages and we'll take in a string array called messages and a float called delay alright when I want to loop through the messages array getting the values one by one so we still have to do that last episode with a for each loop so you can say for each string message in the messages array let's print out that message and then let yield so yield return you wait for seconds and we'll pass in the delay as the amount of times we want to yield now let's create a little string array messages and that sets equal to a bunch of string so make this welcome to this amazing game alright and then we can say stash co-routine print messages and we can pass in our parameters here so passing messages and so we want to a point five second delay between them let's save this go into unity and run and we should see it printing us welcome to this amazing game great so what I'd like to do next is critical routine that will take in a vector3 for a destination and a float for a speed and it will move the object from its current position to the destination using that speed so that's great I enumerator let's call this move and takes an a vector3 destination and a float speed all right now to move our object towards the destination we can say transform position is equal to vector three dot move towards and need to pass in our current position set transform third position in an hour target position that's our destination and then the amount that we can move in this frame so that will be our speed multiplied by time dot delta time now this will just move us once towards our destination what I want to do is keep repeating until we've reached our destination so this is a great place to use a while loop we can say while our transforms current position is not equal to the destination then keep moving us towards the destination until we reach it now we want to wait for a frame between each execution of the while loop so this doesn't just all happen in a single frame so to do that we can say yield return null that will wait until the next frame now to test this ask let's make it so that whenever we press spacebar it picks a new random destination to move the object to so let's crush an update message and we can say if input get keydown keycode space then we're going to stash co-routine move and to get a random position let's just say random dot on unit sphere and we can multiply this by some distance say 5 and then let's also pass in our speed so make a speed of 8 and let's try that out so if I apply space bar we can see it moves nicely too it's a random destination but now what if I mash a space bar a bunch of times well as you can imagine that will start at multiple move co-routines and it also be competing with each other to try and move the cube to a different destination so I'll do that mashed a few times and if we zoom in on the cube here you can see it sort of jittering around and if I press spacebar again it can't really move to where it wants to go because other sets competing with all these other co-routines so before we start in you move coding we want to stop the currently running one if there is one so we do have this stop courage method available to us but in order to use that we need a reference to the currently running move code in so of the Tokyo let's creation ienumerator variable called current move corrosion and when we want to start a new corrosion we can set our current move clarity in equal to the coating as we will be starting and then we call starter routine and we pass in our current move curtain now before we do that we can say if the current move Co routine is not equal to null then we can stop that co-written by calling stop co-routine and passing in the current move close in so let's save that and now if we press play and mash spacebar a bunch of times you can see that it continues to function practically fine I'd like to work through one final example which is to get this cube following a path so I'm going to create a other cube the scaleless down to nice and tiny and this is going to be a waypoint in the path so to define a path we'll have to duplicate this a bunch of times and just spread these out so I'll make it sort of roughly circular paths like so for the like that will be good and then we need a way to store these so inside the test script I'll make a public transform array called path and we can save this and go to the cube and then it compiles get a little path array over here now I'm going to quickly lock the inspector with this lock icon up in the top right here measures means that we can select other objects and the inspector won't change so having selected all of these waypoints we can drag them into the path array and I will be added over there can i unlock this and now in the test script I'm going to create a new Corrosion called follow passed and this is going to loop through each of the waypoints in the path so you can say for each transform Waypoint in path we want to move towards that Waypoint so we obviously make use of our move codeine for that or say stash turret in move to Waypoint position at the speed of say 8 now where if you want to wait for this move codeine to finish running before we move on to the next Waypoint now we don't know exactly how long it's going to take for the move code here to finish running so we can't really use yield return new waitforseconds however we can actually use yield return on the co-routine that we've just started and that's we'll wait for it to finish running so let's go to the start method now and just say start co-routine follow path and let's see if this works so press play and you can see our cube moves between the waypoints so jessic okay so to recap coatings allow one to spread the execution of code over multiple frames using the yield statement you can use yield return new waitforseconds to pause the code in for a certain number of seconds or yield return null to pull it until the next frame or even yield return on a start a routine call to wait for that clarity in to finish running to stop a code in you'll need a reference to the currently running corrosion to pass into the stop coyote and message you can only start and stop codons from the class that inherits from one of the havior that's everything for this episode until next time choose
Info
Channel: Sebastian Lague
Views: 61,504
Rating: undefined out of 5
Keywords: Learn, game development, unity, code, how to, make games, programming, C#, unity3d, coroutine, ienumerator
Id: Eq6rCCO2EU0
Channel Id: undefined
Length: 10min 56sec (656 seconds)
Published: Mon Mar 27 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.