In part 3 of our multi-part game audio programming series, Brennan shows us how to create coroutines for more advanced sound programming within a game. We have included a list of programming terms used in this video below, to help you follow along.
Boolean: A boolean is a true or false statement. You often will use them in if statements to check whether something is true or false. We can create a boolean called “myBool” and default it to false by typing in “bool myBool = false”. Then any time we check the value of it, it will tell us that myBool is false. We can change it to true at any point by typing “myBool = true”. Then it will return true when we check the value of it. You can change the value of a boolean at any time to make your game’s logic work for you. You can have a “TurnLightOn” function change the boolean to true, while a “TurnLightOff” function makes the boolean false. This way you can check whether a light is turned on or off simply by checking the value of the light’s boolean.
Coroutine: Coroutines are a type of function that differ from a standard void function. Where a void function will execute the entirety of its code within a single frame, a coroutine employs yields. A yield will stop the function from continuing to execute code for a certain amount of time, or until a condition is met. Once the yield is over, the code in the coroutine will continue to execute. For example, you can have a function that turns a light on, waits a few seconds, and turns the light off again. Using a void function, this can be a slightly more complex, lengthy piece of code to write. Using a coroutine, however, the code is much simpler, about three lines by making use of the yield functionality..
More from the Blog