Top Down DnD & GML Tutorial in Gamemaker #3 Projectiles

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Here's Ep3 of a Gauntlet inspired series for making a top down game in either DnD or GML. Hope it helps someone make the game they are dreaming of. Any questions, let me know.

👍︎︎ 3 👤︎︎ u/Slyddar 📅︎︎ Oct 18 2021 🗫︎ replies
Captions
if you want to learn how to make a top-down dungeon crawler something inspired by classics like gauntlet and even diablo something that has path finding enemies an area of effect explosions something that was the beginning of my own dungeon crawler game a top-down couch co-op action rpg with randomly generated levels and multiple player characters then keep watching and i'll show you how g'day gamers and welcome back to our drool gml and drag and drop top down dungeon tutorial in the previous episode we've built a moving player with a bow that aims at the cursor in this episode we'll add a way for the player to fire projectiles from their bow and give the bow some recoil after it fires so let's add the projectile that will fire when we hold the mouse button down now in the description you'll find a link to a resource file that contains the resources for the project if you've previously downloaded this you'll need to grab it again because there are two new sprites in the file so i'm just going to drag in the arrow sprite first thing i want to do is just grab the origin and that's the center of the sprite and i'm going to put it down here at 15 9 just to the front of the arrow now i want to change the mask so inside of the collision mask you can see by default it's set for the whole amount i'm just going to change that to manual and i'm going to drag it to be a smaller amount i'm going to just place it at 12 and 7 and then 16 and 11. so the positions are down here and i'm creating a small section for the collisions just at the front here and that's so the collisions are not so wide because when we rotate the arrow at different angles the mask will become larger depending on the angle so i like to use a small tip at the collision mask as this will ensure the mask stays fairly consistent when being rotated so once we've done that close that down and we can create a object for the arrow so i'm just going to call it o underscore arrow and we can drag across the sprite and lastly i'll just hold down shift select both of these and drag those into the player i'm going to add a create event for the arrow and that will run when the arrow is created now the first thing to do is just set up some variables for the arrow and we're going to have a damage variable and i'm just going to set that to 5 and that'll be the damage that it does when it hits an enemy i'm going to have a range variable and i'm going to set it to 160 and that's how far the arrow will travel and then we've got an owner underscore id variable and i'm going to set that to no one now owner id will be the id of the instance that creates the arrow and no one is a special keyword that game maker knows it means we're not assigning it to anyone at the start and lastly i'm going to have a variable called knockback time and i'll just set that to 5 and that'll just be the time that the enemy gets knocked back when they get hit now there'll be a few times when the arrow gets destroyed and instead of creating code multiple times i'm going to create an instance function so there is a declare a new function here drag this in and i'm going to call this arrow underscore die so over in gml we just write function and then the name of the function and our open and close brackets and then our open and close curly brackets so what do we want to do when the arrow dies well we want to go and set its speed to be zero because we want it to stop moving and the other thing i'm going to do is create another instance and that is an explosion when it dies so there is a change instance code block now i'm going to change it into something called o underscore arrow underscore explode so over in gml we can just type the function we write what we want to change this into and there's also another argument if we want to perform events and that means will we perform the create event and the destroy event of the new object so i'm just going to set that to false so that is an object that doesn't exist right now but let's create that as well so we'll take a copy of that now then if we look in the resources you'll find you have an arrow explode sprite let's drag that in and let's set the origin to be the middle center and set its speed to be 14. now if i press play it's just a simple animation that we'll use for when the arrow dies i'm also going to just change the name and take that strip 6 off the name so now that we have that sprite let's create an object for this as well if i right click on an object i can get a shortcut to create object and we want to paste in what we copied before and let's drag in that sprite and drop it there now what i want to do with this is when the animation finishes playing i want the object to destroy itself so if we go to add event go down to other and there is an animation end event if we select that then we can just drag in a destroy instance code block or type in instance underscore destroy and that way we're cleaning up our object when it finishes its animation so we have an arrow explode if we go back to let's drop that into player if we go back to our arrow so now when we run our arrow die function we'll change into arrow explode and after the animation plays of the explosion the object will destroy itself now the reason we just didn't change the sprite of this into the arrow explode sprite directly is because the code in here will still continue to run and if we've got collisions with the arrows they'll still happen it's just an easier way to do an explosion because you don't have to worry about the code of the arrow interfering when you're doing the explosion so now that we have that function let's use that i'm going to create a step event and i'm going to declare a local variable and we're just going to call it underscore dis standing for distance now in game maker we use var to create a local variable and it'll turn yellow to indicate that and what we want to do with distance is find out how far our arrow has traveled so i'm going to use the function called point underscore distance and that takes two points x1 and y1 x2 and y2 and if you give it two points it'll give you the distance between the two points so we want to know how far the arrow has traveled we can use the variables x start y start which are built-in variables that are created when the instance is created and we want to find the distance between that and our current x and y position now once we have that distance we're going to put a check in and we're going to say if that distance is greater than our range variable well that means our arrow has traveled the distance it needs to we can then destroy it so over here we can run a function call and just drop it on the right there and the function we want to call is our arrow underscore die function so what that means is when our distance is greater than range we will run this function we'll set our speed to zero and we'll change into our explosion now the great thing about having that kind of function is we can use it as well when the arrow hits a wall for example so let's go to add event let's go to collision with our o solid and when that happens we want to run the same function so go back in here and i'll just copy that function call and then just paste it here and now whenever we hit a wall the arrow will die as well so the other thing i want to do is go to add event go down to other and there's an outside room event so when the object travels outside the room boundaries we want to destroy it as well otherwise it will continue to travel and your game will build up with objects that you're not using and it'll slow down over time so just make sure if you're not using an object anymore or if it's left the room you need to destroy it so we'll just go here and go to our destroy instance or type instance underscore destroy in gml and that'll make sure it's destroyed when it leaves the room now we need a way for the player to fire the arrow so let's go to our player and just in the create event where we're creating some variables for the bow let's make some new variables here and i'm going to create one for our fire rate and i'll set it to 30 so that's how quickly we can fire and we'll make another variable called can underscore fire and i'll set it to true and that'll just be a boolean which means it can be true or false and that'll indicate whether we can fire or not and lastly we'll have a variable called arrow speed and i'll set it to 8 and that'll be the speed of the arrow when we fire it now we can make a function to check for when the player presses the mouse button to fire the bow and once they're fired we can use an alarm to control the fire rate an alarm can be set to a particular time and it will count down and it will run some code after that time is finished so we'll use it to limit how fast we can fire and at the end of the alarm we'll set our can fire to be true again so let's go and right click and make a new script now i'm just going to call this check underscore fire and if you're in gml you want to just go into your player processor script and you can just add an extra function down the bottom there called function check fire so the first thing we want to check for is are we holding the mouse down so i'm just going to drag across and if mouse is down code block and for gml you want to do an if mouse underscore check button and you have to type the button that you're checking in our case it's m b underscore left so if we are holding that mouse button down we need to check if our ken fire variable is set to true so if our ken fire is equal to true then we can set our ken fire to be false now in drag and drop where you say if can fire equals true now because i'm doing comparison i'm writing two equal signs now you can get away with writing one because gml is fairly relaxed with its rules but it's good practice to learn the correct way and when you're doing a comparison you should be writing two equal signs now instead of writing can fire equals true i can just write can fire because it's a boolean if it's true this is all we need it's a bit redundant to ask if it's true because it'll if it's true it'll run anyway then we can set our ken fire to be false and then we can set our alarm which will reset ken fire back to true so we can go here and set an alarm countdown and we'll set our alarm 0 to be fire underscore rate so back over in the player let's go and add an event and it will be alarm zero and after the alarms counted down we want to set our ken fire back to true now gml also allows a description here and the description will appear over on the alarm this is not available in drag and drop even though i have requested that they add that because that functionality is pretty useful so back over in our check fire script after we've set the alarm and we've set our can fire we can go ahead and create our arrow but first i'm going to grab the direction that the arrow is going to fire so i'm going to drag across a declare temporary code block so we're setting a local variable and i'm calling it dir so underscore dir now before we use this method for our distance and we can do a similar thing for direction with point direction and it takes two positions as well so where we currently are which is our x and y and where we want to shoot which is where our mouse is so our mouse underscore x and our mouse underscore y don't forget your brackets and that will then get the direction from the player to the mouse and we can use that to create the arrow instance so i'm going to drag across the create instance and for the position i'm just going to set it at relative to 0 0 so it's x and y and then the layer we want to create it on i'm going to create a new layer called arrow make sure that's in exclamation marks because it's a string and the object we want to create is that o arrow now the last thing to do is i want to assign it to a target because i want to capture the id of this instance so i'm going to set a local variable so set it to temp and i'm going to capture it as underscore inst standing for instance now that we have that variable we can actually change some properties of the newly created arrow and in drag and drop we're going to use the apply to and over here if we click the little drop down we can apply it to different things and i'm going to apply it to underscore inst which means that everything placed under this code block will apply to the arrow that we created in gml it's a bit easier you can type with and then type the id of the instance you want to change so the things i want to change about the arrow are its direction and its distance and its speed so let's drag across a set instance variable code block and i'm going to change the speed and we're going to set it to arrow speed now the thing about this arrow speed variable is at the moment we're talking about the arrow because we're using an apply to so essentially that's putting us inside the arrow so this variable doesn't exist inside the arrow it exists inside the player this is the variable that we created in the players create event so in order to access the player we have to type other dot arrow speed and that will now get the other object outside of the apply to or outside of the width so whenever you use an apply to or a width you want to use other if you need to grab a variable from outside of the particular instance you're in now the other thing we wanted to set was the direction we want to set it to underscore that was the direction we calculated up here now it's worth noting that we don't need to use other for a temporary variable or a local variable it's available for use anywhere in the event that it's declared in now setting the direction will set which way it moves but we also need to rotate the arrow so it's pointing in the direction that we want it to go and we can do it in drag and drop by changing the image rotation in gml it's called the image angle we want to set it to underscore dir now the last thing i want to do is go and change the owner id now we won't just be using this variable today but i still set it because it's something we'll use later on and we want to set that to other so that's the id of the instance that creates this arrow and this is the way that you can ensure that the arrow only hits enemies and not the player for example so now we can go and create this arrow layer i'm just going to click on instances create a new layer and just call it arrow and now if we go back to our workspace and go to our player's step event we can add the function call to our check fire so i'm just going to do it after we do our calc movement so now let's test the game by pressing play and if we hold down we can fire our arrow but the problem is the arrow looks very static we can give the bow some movement by using the bow distance variable which is how far the bow is from the player so let's change its value when we fire so over in our check fire as soon as we've done the firing let's go and assign our bow distance variable to set it to 5. now if you remember this is currently set to 11 by default so let's press play and test that out so now when we fire we get our bow having some recoil but the problem is it doesn't bounce back to its original position so in order to have it to return to that position over time we can use the lerp function so let's go to our n step where we're setting the bow position and let's use lerp here to change our bow distance back to its original value over time and we can just set bow distance to be the lerp of whatever bow distance is at the moment and our target's going to be our 11 and how fast we want to move it is going to be just 0.1 so 10 percent so that means we're trying to move the variable bow distance to 11 by moving it 10 percent of the distance at a time so initially when we're setting it to 5 the difference is 6 between 11 and 5 so it'll move 0.6 the first step and it'll move 10 of that difference every step now by using this variable 11 it's kind of a magic number because if we change the bow distance in the create event we've got to remember to change it here as well so to counter that you could create an initial variable for this and use it wherever you want to use the 11. it's a bit better practice to do something like that but i'll leave that for you to change now let's just test that out and now we have the recoil of the bow every time we fire and it moves smoothly back to its original position that looks great so that's all for this tutorial if any of my tutorials have helped you and you want to give back to show your appreciation or you just want to access the source code or any bonus tutorials as well as extra videos related to my keepers of pyrite game check out my patreon page as these supporters allow me to continue to post content for you my legendary supporters are kaizer ho andy k colin litchfield bb samurai jose pablo avila medrano and this month's epic supporters are sky devil palm salvatore capolino dan half edward lyko paul a leblanc joking frohold robert churches sinfuliga kareem butler sudu luke lukbanei sirpida and yuta mamaritsu a big thank you to these rare patreons for their support as well so thanks for joining me i'll talk to you in the next one [Music]
Info
Channel: Slyddar
Views: 12,273
Rating: undefined out of 5
Keywords: gamemaker, studio, platform, platformer, drag, drop, dnd, dragndrop, drag n drop, tutorial, peter morgan, one way, oneway, fall, make games, shaun spalding, heartbeast, spalding, yoyo, gms2, gms, advanced, learn, drag and drop, collision, make game, jump on head, enemy ai, horizontal, collisions, coins, collectible, items, objects, slyddar, peter, morgan, 1.4, top down, first game, movement, gauntlet, arpg, diablo, action rpg, bow, projectile, bullet, arrow, shoot, aim, lerp
Id: 58ZtefGUYQE
Channel Id: undefined
Length: 20min 46sec (1246 seconds)
Published: Mon Oct 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.