Make an Action RPG in Godot 3.2 (P10 | Signals + Instancing Scenes in Code)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
good morning afternoon or evening wherever and whenever you are my name is Benjamin and welcome to part 10 to our Godot action RPG series in the last video we ended up adding our attack animation with our attack state but we still don't have anything to actually attack so we're going to be adding that in this video now we won't be getting into the actual hurt boxes and hit boxes for those attacks yet that will be in the next video but we're going to add a grass scene and a death effect for that grass in this video and we'll also be learning a little bit about signals and what signals can be used for which they're very powerful so let's start by organizing our scene here a little bit I want to put all of our bushes into their own little group over here in our scene tree so that when we start adding grass we can have bushes and grass all kind of organized so to do that we can add a new child node here and we can add a node to D and we can call this bushes and we can select our bushes I'm going to select the top one hold shift to select the next one click and drag them and drop them into our bushes folder here right the problem that comes up when we do that is that if we run our game our y sorting no longer works why is that well it's actually sorting these bushes based on the position of this bushes node right here and that doesn't work because they each need to be sorted on their own position so the solution to this is to change this you can right click on the node do change type to its own Y sort note so you can have a y sort node inside of another y sort node and then apparently it's smart enough gadot is smart enough to be able to know oh ok they want to group these together but they want to be able to have Y sorting on each individual one's still so that works quite nicely for doing that the other thing I want to do real quick is inside of our player a lot of you have been asking and this is a really good question so I appreciate it a lot of you have been asking about the difference between the physics process and the process function so the main difference between these two and you can actually look it up in the documentation right we've got these functions in the documentation and you can learn about them you can see here in our process called doing the process step of the main loop processing happens at every frame as fast as possible so this happens the process happens as fast as it possibly can and so the Delta time for that is is not constant it's different and so this is different from the physics process because if you look up physics process if you look up the physics process this is called during the physics process of the main loop process physics process means that the frame rate is synced to the physics that means this function this callback function waits until physics the physics of the game have already been processed and so Delta for this should be should be constant it shouldn't change and the main idea behind that so why is that important right why would they do that why would they have a physics process or a process well the main reason is because if you need to access any variables that are manipulated by the physics engine so for example the position of the player if we needed access to the players position or something else that's manipulated by the physics we would need to do that inside of the physics process in order to get accurate data if we did it in the process we might actually be getting the position of the player on the previous frame because the physics hasn't been updated yet so that's the reason why we have the physics process or the process so in this case since none of our code accesses the player's position or any physics variables we have our own velocity and and stuff like that speed and acceleration but these are actual variables related to the kinematic body these are variables that we have created we're not actually accessing any of the kinematic body physics-based variables and so because of that we don't need to use the physics process we can just use the process and from what I'm told this actually helps with jitter and stuff as well it helps the game to run smoother so if you can stick it in the process event and then you can stick it in the physics if you actually need to get access to the players position or something like that then that is going to be when you want to use the physics process okay so there's just a little bit of cleanup in our project that I wanted to do now let's actually create our grass scene create a new scene do a new 2d scene call this grass and we'll save this scene ctrl s inside of our world save kind of by our bush we'll add a new sprite node to this so sprite node right here and we can drag in our grass texture we'll set the offset we don't want this to be centered and we'll set our offset to negative 8 by negative 8 we can save and now we can place some of this grass inside of our world and we'll create and we'll click on our Y sort scene here and we'll create a new child node will do another y sort and we'll call this grass and then if we have that selected we can then grab our grass scene from down here and place it around in the room oops undo that just like this put some over here and another way to do this is you can actually duplicate it around so I can select this one duplicate it and then drag this around you just use ctrl D to duplicate it and that can be a little bit faster than manually placing all of these so there we go we'll call that good for now and save and run our game now we've got some grass and it also it has some y sorting so you can see we can't actually go behind some of those for the most part we just kind of run top okay and now we will now that we've got those organized nicely you can actually hide or show them all at once we'll come into our grass scene here we'll add a script to it and that should save underworld grass Gd and we'll give it will uncomment this process function you can use ctrl K to uncomment it and then we can say if input is action just pressed attack Q free so what does the Q free function do well it adds this node to a queue of nodes that should be freed or basically removed from memory destroyed from the game and it does that it doesn't remove them right away just in case there's other things that need to access them on this frame so it usually waits till the very end of the frame to remove those from the game it's different from the free function which will remove it right away but Q free is generally what you're gonna want to use so now when we run the game and we press this all of our grass gets destroyed so we've got the most Oh P attack in the game right now but we're gonna want to set up an effect for when the grass is destroyed so let's create a new scene here I'm gonna do a 2d scene we'll call this grass effect and this time oops oh yeah we'll save this save the scene control s but let's come back into if you can press this little button up here to go back we're going to come into our effects folder and save it inside of effects now we'll add a new sprite to this but we're going to use an animated sprite for this now we didn't use an animated sprite for our player because we wanted to be able to use the blend space 2d and so and like the animation tree and stuff like that so because of that we didn't use the animated sprite but you can use animated sprites and I'm using it here because this is a lot simpler of an animation it doesn't require lots of different things that's just one little animation and that's where animated sprite shines so we can create new frames over here do new sprite frames and click on that and that will open up this little window this is an animation that we've created we'll just call this animate I guess because it's just going to be the grass kind of falling apart in the air the leaves kind of fluttering and we'll set our frames per second to 15 here now we need to add an animation we need to add animation frames to this animation right here so we can click on this because ours is from a sprite sheet and then we come into effects and we select our grass effect PNG and that will open up this window this you can't zoom in here so this is a little bit rough still it's a slightly newer feature to Godot I don't have a lot of experience with it yet but right here these numbers right here represent how many frames there are so you can see vertically we actually only have one frame vertically right there's only one row here and so for this animation and so we want to set this down to one and horizontally it's hard to tell because of the kind of animation that we have here but I know that this animation actually has five frames horizontally so now you can see we've got one two three four five now in order to continue we need to select these frames now you can manually select them just in case you know you had a giant sprite sheet and you only wanted some of the frames you didn't want all of them you can manually select them but in our case we can just select them all using this button up here and to add five frames now we have each of these frames inside of our animation and we can you we can turn on playing here and this animation will play and we've got loop set to true so it plays over and over and over and over again we're gonna turn off loop we're going to turn off centered and we'll turn off playing for now and then we'll set our offset to negative eight and negative eight just like the grass that we have the grass scene that we have over here so now we've got this sprite animation set up on this animated sprite but we need to actually play it so let's come into our grass effect scene over here we'll add a script that should be added under effects grass effect and we'll just call this well actually instead of calling it grass effect let's just call it effect for now well we'll leave it grass effect we'll probably end up changing that later so create and then inside of here we're going to get access to our animated sprites of our on red Ivar actually animated sprite equals animated sprite now what can we do with this animated sprite well let's look it up in the documentation animated sprite what kind of functions that we have we have play with an animation we have stopped as if is playing so those are kind of the functions we can do we've got these different properties we can manipulate but we don't really need to worry about those actually so all we're going to do is inside of our grass effect in the ready function will call animated sprite play animate and then we can run the game well actually we don't want to run the game we want to run this specific scene right here to make sure that this is working we can do that by pressing this little button right here or hitting f6 and I couldn't tell if it actually played animated sprite play we didn't get any error messages let's do I want to be able to test this so we'll do process function will say if input is action just pressed attack and then we'll copy this line right here and paste it right here and run this scene and now if we press the space key it should animate but we're not seeing it animate so something is wrong here maybe we have to have playing set to true let's set let's turn on playing again try this one more time we're still not getting it actually playing so I'm guessing the problem is since we're not on loop here it's actually it actually is playing it whenever we do that but it's not starting on the last fret on the first frame it's starting on the last frame that's my suspicion here so let's see if we if our play function takes some sort of properties so we've got backwards we we pass in the string here so it has a frame that is set to the default is set to zero we might need to reset that frame so let's try doing that real quick animated sprite dot frame equals zero so we'll see how this goes let's see how this goes and there we go now it's working properly so essentially it was playing but it was starting on the very last frame so what we want to do here is set our frame equal to zero right in here we can get rid of that code now we don't need it so once once this is created they will start on frame 0 and then it will animate our sprite and then what well we want to get rid of it right so it animated there it's hard to see but it did animate there well we want to get rid of it but we don't want to get rid of it until the animation is finished so the animation is over and this is where signals come in so on our animated sprite note here if you click on it instead of having inspector come over to node and you should see these two tabs called signals and groups we're not going to worry about groups right now but we do want to worry about signals and you can see under the animated sprite we have an animation finished and a frame changed signal so these signals they're basically signals that get they're kind of like messages that get sent out like a text message or something they get sent out whenever something happens it says hey this thing happened you know like your friend has a baby or whatever he sends out a message to everyone and says hey I just had a baby right that's kind of what it's like and so we've got animation finished here this signal is the one that we're concerned with because once this animation is over we actually want to destroy our grass effect so we're going to double-click on this animation finish signal and it will open up this window and it says connect a signal to a method so this is a little bit scary looking signals can be scary but basically whenever a signal fires you want to connect that signal to some sort of function or method so that when that signal happens when the text gets sent out essentially we do something right what do we want to do well we want to send a text back and say congratulations to our friend you know for having a baby so in this case though what we want to do is call this function right here this is the function we're going to be creating and this is the function that will be called whenever our animation finished signal gets sent so we'll do connect and you can see it's opened up our grass effect Gd script and it's added this function so now we can set up what it's going to do and we're going to do Q free so when this animation finishes this grass effect will destroy itself and you can see now there's this little icon in the editor for the signal that we connected and if we click on signals here we can see that there's it's been connected we've get this information and the function it's been connected to and we can double click on that and it will bring us to that location in this the the code editor here and we can also see that it's been connected here we can see that this is a signal we can click on that and get information that the source is the animated sprite node the signal that this is connected to is the animation finished and the target node that it's connected inside of is the grass effect right here so let's run this scene again see if this works and yep at the end it actually gets destroyed so one more thing we're gonna learn about here is actually instancing nodes encode because we're instancing scenes in code because we know how to instance a scene inside of the editor here right we've done it before we clicked on the grass drag you drop it in the editor well how do you create the the effect right our grass effect inside of the game while the game is running because we want to create it when our grass dies so let's come into our grass script here and we're going to create our grass effect scene right here inside of this grass Gd script once the grass is destroyed right right here once it's been attacked so first we need to create a variable and we'll call this grass effect and I'm using a lowercase G here I'm using camelcase here basically a lowercase G here and that's because generally with my scenes I name them with an uppercase here so this is an instance of a scene so I'm naming it lowercase here well actually we're gonna load the scene first so let's do uppercase ironically grass effect equals load and then we need to pass in a path for the scene that we're going to be loading and well we want to load our grass effect so that's going to be under effects grass effect dot T SCN and it'll be under this this path right here okay well that's kind of a pain to type out but in Godot you can actually come into your file system go into here find the grass effects seen grass effect T Sen drag and drop it right here and that gives you this the exact path to that scene okay so once we've loaded this scene and stored it inside of this variable we now need to create instance of it so we can say var grass effect equals grass effect dot instance okay now this is a little confusing because we've got uppercase grass effect here lowercase grass effect here why well this is the actual scene right here this is this is called a packed scene so it's a scene it's not actually a note but this right here this variable is going to have an instance of that scene which will actually just be a node right so it will be its own node where this is a packed scene it's a different kind of data so once we instance that scene and store it inside of grass effect well then we actually need to put that grass effect inside of the room like inside of this scene right so think about it this way when we bring in our grass effect like this and drop it in this node gets the scene gets instanced and then dropped inside of here as its own as an instance of that scene and we know it's instance because you can make multiple of them right but it has to be added as a child of some other node in the scene it has to be added as a child so we have to tell it to do that in code if we come back in here we can say we could add it as a child of our of our grass that we're on right now but we don't want to do that because the grass is about to get freed so then it would it would destroy our effect we want to add it to the root world node or maybe our Y sort node and that kind of depends on you which one you want to add it to but for me I'm just gonna add it to the root world node so how do we get access to the root world node inside of our script we can save our main equals get tree dot current scene so this gets the current route main scene of our scene tree okay and that's a little bit scary it's a little bit hard to understand but we can actually see this in action so if you run Godot okay and then you come over here you can see you have a local scene which is the scene in the editor but you also have a remote scene now this is the actual scene that the game is currently running this is what the game actually looks like over here you can see we have our world scene we've got our background dirt paths are why sort it's all the stuff and if you click on these like if we click on player and go to the inspector we can actually see information about our player in real time all of this stuff in real time that is that's this isn't the scene that we made inside of our editor this is the scene that's actually running in the game right here so if we actually move our player over here we can see our player's position updating down here in the corner in real time so we can see exactly what's happening inside of the game so this right here is called our scene tree so when we do get tree it's getting this right here and when we say current scene so get tree current scene it's actually getting the very first scene that it has right here this world scene and so that gets us access to world so we can call this main or we can call it world whatever you want to do probably for consistency I want to call it world here so let's stop running the game and that brings us back to here and then we can say world add child and the child that we want to add is our grass effect now make sure this is a lowercase G because this is the instance of our grass effects scene you can't actually add a grass effect scene to the world note it has to be an instance of it okay and once we've done that we can actually run the game and test this so let's run the game and test it okay so they all got destroyed here and we got a grass effect but maybe you guys saw it maybe didn't it was actually up here in the top left hand corner well that's because when the effect gets added to the world that's being added at position zero zero so we also need to set the position we need to tell it where in the world this grass effect should be and we'll do that by saying grass effect dot position global position equals global position now this global position is the global position of our grass so we're setting the grass effect global position to the global position of the grass now when we run the game and we swing our sword we get the grass effect at the position of all of the different grasses and that's the effect we want that's the effect we want so learning how to instance scenes inside of code is very important if you want to do anything inside of Godot like if you want to have a gun that shoots or if you want to whatever it is you know you're going to need to learn to instant scenes inside of code and this is how you do it you load the scene this is step one store it in a variable you instance that scene this is step two store that and its own variable you add that scene well well this is getting access to the world but you add that scene as a child of whatever node you want to add it to in our case we wanted to add it to world so we had to get access to the world first and then you said it's position generally so those are the steps in order to add it to the world and so I hope you learned something in this video this is going to be it for this video we did quite a bit we learned some new things and we're ready for our hurt boxes and hit boxes video where we can set up actually attacking individual pieces of grass instead of just destroying them all at once with our overpowered move so this video is made possible by my Kickstarter backers for the one bit kit of course I'll put information about that in the description and at the end of this video thank you all so much for your support if you enjoyed this video give it a like subscribe to my channel and I will talk to you all later
Info
Channel: HeartBeast
Views: 84,902
Rating: undefined out of 5
Keywords: Godot Engine, Godot 3.2, Godot 3, Tutorial, Series, Action RPG, RPG, Pixel Art Game, Indie Game, Game Development, Learn gamedev, Gamedev
Id: 1mI04gPhd3E
Channel Id: undefined
Length: 28min 54sec (1734 seconds)
Published: Wed Apr 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.