Gamemaker DND Platformer Tutorial - #10 Damaging the Player

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you're interested in making a detailed platformer just like this one with in game maker using drag-and-drop then keep watching good a gamers in this tutorial we're going to look at a way that enables the player to take damage now last tutorial we added a way for the enemy to take damage so if you haven't seen that you should watch that first because we use a similar system to do the player damage now before I start I wanted to mention my udemy course uses a similar system but it's much more detailed so if you enjoyed what we've implemented here you should check out the course there's a discount in the description that will give you 90 percent off the course it's currently the highest rated game maker to course on udemy it's definitely worth checking out now the first thing I want to do is add a restart button to our game so if you're going to again and let's add an event let's add a key pressed event and I'm going to add a letter R and we just want to restart the game so over in the toolbox I can type restart and just down here I'll find the restart game it's just very handy if we want to test things we can press R and we'll go back to the start so while we're in game let's go to the create event and let's add a new state for our player I'm gonna put a comma and then we're gonna add a knock-back state and we're going to add a dead state now let's go to our player and let's have a look at the collision between the enemy and the player now if we look at this from last tutorial there was actually a mistake here I had put greater than it actually should be less then I chosen the wrong one it should be less than for this test we want to make sure our beer box bottom is less than this value because less means higher it will still work but we want to make sure it's less because then we'll get the right bounce at the right height now in order for the player to take damage we need to do some combining of our checks here because this check makes sure that we're hitting the head of the enemy and this check makes sure that we're going down but if neither of these are true well then we'd like the player to take damage and because we can't put an else on both of them we need to combine these into one check so we can do that by using the if expression so I'm going to drag that across and here we can type multiple expressions that we can check your true or false but here we're just going to take a copy of this so I'm going to take a copy of that and we want to make sure it is less than the value down here it's a copy of that and paste it in so essentially this and this are asking the same question it's just that we've combined the whole expression into one line and by doing that we can press the plus and we can add another check and here's where we want to check lb SP so we want to see if our be SP is greater than zero but these two are now combined into one so we need to move all of this up to the empty spot here of the if expression so now we're checking if these are true then we're going to do this so we can delete these ones so if this is not true well then the player needs to take damage now currently what we're doing is we're making sure the enemy's not dead before we deal damage to the enemy but the player also needs to not be dead for any of this to happen so I'm going to drag across another variable check and we're just going to say that if the state which is the player's state is not equal to PS dead but now that we have a dead state in the player we're making sure that all of this can only happen if the player is not dead as well so let's take all of this let's take all of this and let's drag it over here to the empty so now we're checking but the player states not dead the enemy state is not dead and then we're carrying out our collision checks so we need to add an else in here so here let's add an else and we'll say that if that is not true well then we want the player to take damage but we created a knock-back state and we created that so that we can move into the knock-back State when we take damage but we don't want to take damage if we're already in the knock-back state so the first thing we want to check for is if our state is not equal to play a state dot not back and if it's not okay well then we can move to our knock-back state and a knock-back state is just going to be a state where the player gets knocked back from the enemy and they take damage but when they're in that state we're not able to take damage again and we do that so we don't keep getting damaged from the enemy all the time it gives the player a chance to recover so the first thing we want to do is we're going to knock our player back so I'm going to sign HSP to a value that pushes it away from the enemy now how can we push it away from the enemy well we need to know if the enemy is in front or behind us and then we apply our HSP to push us away so in order to know if the enemy is in front or behind we can look at the sign of our X minus the other X so our X minus the enemy's X now this statement is very handy because it'll tell us if we're to the right or the left of the enemy now if we're to the right our X is going to be bigger than their X so the sign will be a positive number if we're to the left then our X will be smaller than their X and we'll end up with a negative number so if then we multiply that by a positive value it'll push us away from the enemy but I'm going to times it by a variable called HSP knock-back now we haven't created that yet but we will create it in a moment now as well as knocking the player to the left or the right I want to knock them up into the air as well so let's take another assigned variable and let the Sun as VSP a value as well I'm going to assign it to negative VSP knock-back and we will create that as well now the last thing to do is change our state we want to change our state variable and we want to change it to Pierce dot knock-back so we'll be knocked up and to the side and will be changed to the knock-back state but let's create the HSP and the VSP knock back I'm going to do that in the entity because this may be something you want to use for the enemy later on let's go to add let's put in a hey chespin knock back and I'm going to set that to eight and we're going to add a VSP knock back and I'm going to set that to five now you can play around with these numbers so you can make the player get knocked back further or not as far it's up to you but let's go back to the player and let's go back to the step event and let's add a knock-back state but at the moment we have an idol a walk and our air I'm going to take a copy of the air I'm going to copy and paste that and then we're going to make that our knock-back state so why we're getting knocked back the great thing about states is that we can take the controls away from the player so they can't move during that period so if we delete the get input they no longer can move during that time we still want to calculate our movement we still want to check our ground and we want to check our state but we don't want to check it and take them out of the knock back state until we're actually hit the ground so we want to cleave our collision and our animation we're going to make a change here so let's drag across a check and let's say that if we are on the ground and if that is equal to true then we can check our state so we'll be knocked back into the air and when we hit the ground will then change from the knock back state to either idle or to walk now there's one more thing you needed to hear and if we run this as it is this check of on ground is actually already true when we go to the knock back state because if we're on the ground and we get hit this will actually still be true so I'm gonna add another check here I'm going to say if our VSP is greater than zero which means that we're not being knocked into the air and we just need that check here because the way on ground gets calculated we want to make sure we've been knocked into the air and we're coming back down and we've touched the ground so while we're in the knock back state we need to make sure that we're assigned the right animation so let's go to our anni we're gonna take a copy of the air state because that's pretty much what we want to do when we're getting knocked back but where we're setting our image index we're going to use that again and when we use code again it's better to create a script and just use the script so what I'm going to do is cut out this check where we're setting our image index so I'm going to select those fall and cut them out make a new script we're just going to call this check air index let's paste those in there that sets whether our image index is going to be 0 or 1 when we're moving up or down back you know annum now drag across our execute script and we'll just select that check air index for now let's duplicate this case a copy paste and this is going to be our knock-back now for a knock-back I'm not going to do a check of the facing so let's press f5 and we test that out but now we'll move and touch the enemy we get knocked back we still hit them and damage them we can press our to restart the game I'm going to test that again we'll try it from this way and then we get bounced back now if we also do it when facing the other way we're not but we're not changing direction and that's what we want to do we want to make sure when we're not back we're facing the way that we got knocked back great so let's set away now for the player to take damage let's go back to our entity and let's add a new variable and I'm going to add a variable for the HP so I'm going to call it HP max so that's hitpoints and that's our maximum hit points so I'm going to set that to three and then we just add a variable called HP and we're just going to set that to HP max maxximum showing what the hitpoints maximum value will be and how HP will be less than that but we're just setting it to the maximum to start with now let's go back to the collision event with the enemy and right here when we're getting damaged let's take one off that HP value so I'm going to set HP to be minus one relative and that will take one off but that's our HP being decreased but now we need to show it on the screen so let's go into the drawer event and let's add a health bar that shows how much help the player has so if you type in health there is a drawing event for just drawing a health bar and now the value is the value we want to show but we want to show our HP as a percentage of our maximum HP and we can do that by saying HP / HP underscore max times 100 now this section is for where we want to draw it now I've already found a pretty good spot that I want to draw it in I'm gonna put those values in and you can change them if you need I'm going to put mine at minus 20 minus 45 20 and minus 40 and these are all relative to the player so they will be values from the player's origin now the background color I'm just going to select black I'm going to leave it white now min and Max are used to show what color the bar will be when it's full and when it's near empty so I'm going to set the minimum to be red because that's sort of a dangerous time you when you're down there and I'm going to set the max to be green now the other thing is I don't want to draw the health bar if my health is zero I don't want it to be seen but I'm just gonna go and put another check here and say if our HP is greater than zero then we want to draw the health bar let's press f5 and test that out above when we touch the enemy it goes down it changes colors a little bit and the more we get hit and then it disappears when we go to zero now at the moment we don't have a way for us to die so let's add that in let's create a new script and I'm just going to call it check HP and we want to have a test or a check here and we're going to look at our HP and we'll say if our HP is less than or equal to zero well that means that we want to move to the dead State so our state is going to equal player state dot dead now this health check we need to add it to the player if we go to our step event and we go to all our states we need to add it here in the idle State but I'm going to do it just after we do our check state I'm going to copy and paste check state and then change it to now check HP now we can take a copy of that and I'm gonna paste it also for the walk so just under check state all right is there and also for our air we want to do it after do our check state now for our knock back as well now it's very important where you add it if I add it just here below the check round like that it'll change to the dead state and then we might do a check here and we'll be moved back into idle or walk so it's really important that you notice where you're adding the check states you got to make sure that the states are not going to be changed by something else so adding it here is not going to work I'm gonna put it down here and this way that'll be the last thing that's checked but now we need to add a dead state for the player let's go into the enemy because we already have one for the enemy let's take a copy of this one I'm going to copy that go to the player and then paste it here down the bottom now we need to change the es into PS and we need to change the script to make sure it runs the player script which is the annum now I'm also going to add another script here at the top and make sure we are executing the calculate movement script and that'll stop the player moving when they die now lastly when the player dies there's no way to restart the game but let's just add a key press and just here so if our spacebar is pressed we want to restart the game now when we're dead we need to add an animation if we look at our sprites we don't have a dead animation yet let's duplicate the idle animation I'm going to call that s player underscore dead and if you get the sprites from the description you'll see that one of them is the dead sprite I'm going to drag that in and you can see this is our animation to play dying now at the moment the origin is in the middle and because this fights a lot wider it's not going to work we need to place the origin manually I'm going to set it to 15 and 34 and that'll imposition it in the right place let's go to our animation and we need a dead state here for one of the animation cases so copy this one paste and this is going to be for a dead change to SPR did and we don't need to check our index for this one but this SPR dead if we go to our player and we go to our variables we haven't set that yet so let's go down and find that and here it is here let's edit that and we want to say s underscore player underscore dead let's test that out now so now when we take damage and go to zero we should play our dead animation and we can press space and jump back up let's just try it again but face the other way and it knocks us down to the ground so you'll see we still take out we still move up into the air which is what we want but we can't also move at that point we have no control when we use our keys so because we don't run the get in put it in now knock back state or a dead state now let me show you a problem with the way the game runs like that we go to our room we take our enemy I'm going to copy and paste the enemy and move them just over here may I put two on top of each other and let's run the game again now if we jump on top of the enemy's head we take damage even though we kill one enemy we take damage from the other enemy because our collision is happening on the same step as we touch one enemy we're damaging them and jumping in the air but then we're also colliding with the other enemy so what we need is a way for the player to be immune when they jump on an enemy's head and then immunity needs to last just for a few steps in order to move out of the collision of the other enemy now let's go back to our entity let's add another variable and I'm going to call it immune time I'm just gonna set it to ram underscore speed times 0.1 now room speed is 60 and we're doing point 1 of that so this is going to happen for six steps only which is enough time for us to move out of the collision with the other enemy let's get our player and let's go to our collision close this down and move this over so here's where we need to set our immunity now we can do it with an alarm now game maker uses alarms as countdowns but we can set an alarm and it'll count down one per step and it's really handy when you need something to happen over a set amount of time but let's look for alarm and we can set an alarm countdown and we want to set alarm zero to be immune time which is what we set before but now that we've created an alarm and it has a time when that alarm is active we don't want to take damage so let's go down make a change here to say that make sure where we are not in the knock back state and that our alarm is also not active now we can add an expression here and we're just going to say if our state is not equal to PS dot knock back at their first check and the next one is we want to make sure our LARM is not active and we can say alarm open square brackets 0 close fear brackets is less than or equal to 0 and that means that if it's not active because the alarm is counting down when it gets to zero or below it's no longer active but if those things are true we'll then we can take damage so let's drag this up and set that there we don't need this one anymore and now the last thing we need to do is actually create the alarm so it's got to add a vent go to alarm zero and just creating the alarm is not enough the game won't recognize this as a valid alarm if it has no code in it so what you can do is just put execute code block and just add a comment here you can do a comment with two forward slashes and we'll just say this is our player immunity let's press f5 and test that out when we jump on the enemy we should be able to not take damage and we can kill both of them so you can then add a bunch of enemies if you want to test this out copy and paste quite a few of them select multiple ones and put a heap of them in and then you can test it out and you'll see that you'll take damage only when you are hitting them not from above though that's working great we do take damage though but only when we're hitting them from not above great now that's been a long tutorial but we have covered a huge amount of things you can take what you've learned here and then modify it and change it how you need so thanks for joining me I'll talk to you next time [Music]
Info
Channel: Slyddar
Views: 9,177
Rating: undefined out of 5
Keywords: gamemaker, drag and drop, dnd, collision, platformer, platform, make game, jump on head, enemy ai, gms2, drag n drop, drag, drop, dragndrop, tutorial, peter morgan, one way, oneway, fall, make games, shaun spalding, heartbeast, spalding, yoyo, gms, advanced, learn, vertical, parallax background, parallax, enemy, damage, kill, hurt, mario, jump, damage player, dead state
Id: D4_cRbmjahs
Channel Id: undefined
Length: 21min 54sec (1314 seconds)
Published: Wed Jun 05 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.