How to Code a Turn-Based Game: Godot Turn Queue Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this story all we're going to create a turn-based system a turn cube just the node that's going to manage return orders in any type of game this could be on a board this could be in the JRPG that while making that's the way they turned Q works and we're going to make it using good O's tree so let's create that right now from scratch it's a step by step tutorial so we're going to create a 2d scene but note that this works in 3d as well and we're going to name the main node the first one turn Q so there are a few ways to manage the order of turns in general you want to store a collection of objects characters whatever in an array then you want to point at one of the characters in the array and make it active and when it's returning control when the turn is done you move to the next one and you cycle through the array now when you have a node in the tree in the scene tree its children is an array it's a list of elements and we're going to use that to use the tree as our array as a list of nodes with a specific order we're going to use our icon the good but to create a list of characters that we will use here so I'm going to place the icon in the Edit term and we're going to duplicate it two times I'll call this go bots one well it's just called you go bot copy the name and I'll call the other two the same way and we're going to offset them a little bit in the view just to make it clear that there are different nodes that we can work with all right so now everything is going to happen on the turn queue with very little code let's add a new script to it I'm going to make it empty and save it as turn qgd so first we are working with good old 3.1 alpha two at the time of recording but the final release shouldn't change too much and I'm going to use the class name keyword to create a new class that will be read in Godot that we can access from other scripts and call it turn queue like my node and like the scene or going to crate out of it so here's how turn management works we're going to store an active character in a variable active character is going to be one of the nodes children at the start of the game it's empty and we're going to initialize it in an initialize function I recommend to work with functions like these that can call from a parent node from a parent system once all the nodes in the tree are ready this prevents bugs with initialization so this one you have to call manually from another node right as you know turn queue like that dot initialize like this you'd have to call this function from a parent node but it's the same ideas using the ready callback we're going to set active character to the first child so first place where were using the tree the first Gubar the first icon will be our active node at the start of the game and then we're going to create a blatant function that's going to start the turn change the character and cycle just will be able to call it every time we want to start a new turn and give control to a new character so let's create that play sine function we're going to start with the yield keyword the yield is going to pass this function until some nodes or some function is finished on some other node in this case we're going to wait for the active character to finish the turn so this play turn function doesn't exist yet we have to add it but we wait for this blatant function to be completed this completed string the second argument a in yield is a special case where you say I want you to wait until this function finished and returns a value so in our character script we'll code a blatant function that will also use a year keyword and that will wait for some animation to finish for the player to select some action in the UI or whatever it depends on your game obviously but we're going to wait for the character to play its turn and then we're going to go to the next child in the turn Q so go about - and go about three and we're going to cycle back after two turns to do that we're going to get the index of the current active character and we're going to increment it by 1 then make sure that we don't go past the end of the cue cycle back to 0 when we get to it and we're going to store that in a new index variable this one is going to be an integer it's going to be a whole number and we're going to take the active character we're going to get its index in the tree add 1 to it make sure to use modulo get child count here so currently we turn Q has three children GoBot has an index of zero we increment it to one and we'll make sure that it's less than three so when we get to go about three for example the index is going to be two we're going to increment it by 1 so we're going to get to 3 3 is past the end of the queue so modulo the queue size we're going to get a value of 0 and cycle back to the first GoBot and then we set the active character to get child and we use our new index value to get the next journal in the queue so that is how you create a turn queue that cycles from one character to the next here we are in the JRPG project the open JRPG project and we're going to talk about the case where you want to sort the elements in the queue sort the characters based on their speed so if I open the fighter class the JRPG you can see that every character has a speed value and I want the characters with the highest speed to combat first to attack first in the game in this case even if the monsters and the characters are in a random order in the turn queue when I play the game I press f5 you will see that the first character who attacks is going to be blue yeah and then it's going to be dance I can attack once and then stand but in the tree in my game in the editor da at the bottom of the queue so we are sorting them through the code and you can double check in the remotes entry that blue and Dan are before the porcupines I'm going to open the turn cue script again the code is open source you can find a link to the project in the description here we are mostly interested at the changes in the initialize method and saw battlers play turn works exactly the same as we have just seen in the earlier part of the video so in initialize I first get all of battlers I'm actually getting a list of these five nodes the five children of turn queue but I've wrapped it in a method here and I install that in a variable then I call the new sword custom in guru 3.1 this is a method on arrays that lets you solve them using a custom method a custom condition so this one is going to take an object and the name of the method to call on that object then you pass two elements to that method a and B two elements in the array and you return a boolean true or false you return true that depends on how you want to solve the array but depending on your game in my case I return true if a is bigger than B so if a should be before be in the final salt older now salt custom is going to solve the array in place so as you can see I'm just calling the method and it's going to modify the order of the battlers in my battlers variable in Sawle battlers i return true if the first element speed in the comparison is higher than the second element speed and this method is going to get called multiple times until the full array is salted now the final thing I'm doing and this is why while not following the Ducks here the Ducks would tell you to return true if B is higher than a but we want to use the race method on the battlers and raise is doing this it's taking a node and it's going to put it at the first position at the first children of its parent element and so when we saw the array this way we're going to get it in the reverse order so we're going to get the porcupines first and then we go through all the nodes so we raise the porcupine first multiplying to porcupine three blue or in this case it might be Dan and blue and as we raise them all from the slowest to the fastest element to the fastest battler it's going to give us the right order that we're looking for in the game this might be a little confusing but I invite you to check the project you will see that it works and this is how you can not only have a turn-based game but you can salt the turns based on a given condition and all that with in total about 15 lines of code the last thing I want to show you is how we handle this Layton method here so we use the yield keyword to pass the execution of the game and the combat system is waiting for this blatant method to return to then call it again and start the next end now we are calling play turn on the battlers let's see how this one works battlers have quite a bit of code but we're only interested in the blatant method yeah this one is going to do a series of yields so you first have the skin that's going to move forward this is the animation when the character moves forward on the board then we execute the action so this is going to be the action that the player selected in the menu in this version of the demo the very early prototype you can only attack so the action is always going to be attacked this could be a magic spell this could be your special move a limit breaker whatever you can imagine on the character and it's going to call execute on that action which all combat actions have then we play another animation once the action is done so let's say we character executes and attacks it's going to move to its target we're going to wait for one second it's going to damage the enemy and then we play the return to start animation again very early version of the project so right now the motion is hard-coded in the character and as we add more animations more characters will replace that with a more elaborate system but it's just to show you that every time while waiting for another function to complete and these functions like move forward for example it's creating an animation on a twin node and it's waiting for it to complete - then finish and return control to that later and method so yield allows you to create some kind of timeline in your code to work over time it's a really powerful keyword this is how you would create a turn-based system atan manager if you want or as we call it the turn queue that's said willing to explore more topics in the future regarding this RPG project but for now thank you Gary for watching be creative have fun and we'll see one another in the next one bye bye
Info
Channel: GDQuest
Views: 71,384
Rating: undefined out of 5
Keywords: godot turn based tutorial, godot turn based rpg, godot 2d rpg tutorial, godot jrpg, godot rpg, godot 3.1, gdscript, turn based game, godot tutorial, godot engine, game development, game creation tutorial, open source, open source rpg, rpg game system, godot rpg battle system, godot rpg engine, godot rpg 2d, godot rpg template
Id: FV4JkwI4OF4
Channel Id: undefined
Length: 12min 42sec (762 seconds)
Published: Fri Nov 23 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.