GameMaker Studio 2: Action RPG Tutorial (Episode 30: Damaging the Player)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody and welcome to episode 30 the big three zero congratulations again to everyone who has stuck it out and made it this far in this episode we're going to allow our slime buddy to finally get some revenge on the player you can see he's able to headbutt the player make him flash red take uh half a heart of damage and also become briefly invulnerable for a short length of time so we can link uh you can see when i get hit there i can actually pass through him so the first thing we're going to do is do a little bit of setup in our player object so find oplayer go to the create event and just in and amongst your uh your variable definitions we're going to add three new variables i'm going to add invulnerable equals zero okay that's zero not false um that's going to be while it's above zero we're going to be considered to be invulnerable so if we set it to 60 it will mean for the next 60 frames we are invulnerable and that's the whole way we're going to use that um the next variable is going to be flash equals zero similar to our entities in the way we made them flash but i'm also going to put in a variable called flash shader equals sh y flash all right before we just reference the the white flash directly because that's the only flash or entities we're going to use but we're going to put in a red flash um into the game for the player but we want the player to be able to but there might be circumstances in the game where we want to use the white flash as well so what we're going to do is we're going to have a variable in which we store whatever flash we're currently using and we can just allows us to easily swap between different shaders that do different colors or different effects i am trying to keep things still fairly simple wherever i can so that's why we're gonna have just different shaders for different colors rather than have like one shader that takes in a color and and does the flash for us which would be the more ideal way to do it this way is very simple very easy to understand and it works and it's perfectly flexible enough next up we want to make sure that our invulnerable variable and our flash variable decrease over time towards zero so that when we set flash to be something or set and vulnerable to be something they'll decrease over time towards zero so we'll do that in our step event which happens every frame we don't care what state we're in so we don't have to go through all of our different states and uh and and plug that into them we're going to decrease our invulnerable and decrease our flash no matter what state we're in um the only condition is that the game should not be paused so where we've got if not game pause we're going to widen this into a code block instead of just one line and not only are we going to execute whatever our state machine is we're going to do invulnerable equals max invulnerable minus 1 0 and flash equals max flash minus 0.05 or zero okay that'll decrease flash by 0.5 each frame and invulnerable by one each frame but it won't drop them below zero okay because it's always going to return whatever's bigger with that done we're going to implement a little flickering effect whenever our invulnerable is above zero okay and what we're going to do in order to do that is uh in our draw step where we draw our sprite we're basically just going to skip this every now and again while we're invulnerable so up here i'm going to write if invulnerable is not zero and invulnerable mod eight less than two close bracket equals zero close bracket and also flash equals zero so assuming our invulnerable is not zero and our flash is zero because if our flash is above zero we want the flash to take priority over this um this flickering effect uh so assuming we need to care about this at all uh we're gonna check to see if invulnerable mod eight is less than two mod returns the remainder of a division so if invulnerable was 16 eight goes into 16 twice with nothing left over so it would return zero um if invulnerable was 15 um it goes into it once with seven left over so it would return seven and then it would return if it was uh 14 it would return six thirteen five and so on and counting down over time okay um so what this will do is make it so every eight frames um invulnerable will be zero and we'll count down from uh from eight to zero every eight so again to be clear just like counting down from 16 again that was a useful one it would return zero at 16 then seven then six then five then four then three then two then one assuming we're counting down okay so that's uh 16 15 14 13 12 and so on okay and then we get back to zero and then back to seven six five four three two one okay so as invulnerable ticks down uh getting mod eight is making it follow this pattern okay and it would follow a similar pattern if we did like mod four uh 16 would be zero then three then two then one then zero then three then two then one okay another very commonly used one is um mod 2 because then that gives you 0 1 0 1 0 1 right if you do a number mod 2 it means like every other value is going to be 1 and every other value is going to be 0 right which is very useful and uh modding something with two is a good way to tell if something is even or odd right so it'll give you this depending but as you have probably started to notice by now doing mod and a value is a good way to set up a reliable pattern that happens as a number increases or decreases over time right um and so by doing a vulnerable mod a and then checking if it's less than two it means we get this little window every time okay let's add that zero on the end there as well um and if we were to go over this this would obviously be two one zero so every little two one zero we come into this becomes true so you can see like for three frames every eight um this becomes true okay so that just creates a little pattern for us of true then true then and true then all right um which gives us uh the window we need to make our sprite skip in order to do that uh so assuming all this is true we just have a little blank code block here that says skip draw and then l-sit with our draw all right so if all this is true then we just don't draw anything and if it's not true then we just keep drawing the sprite as per usual and now we can test this is working by just going back to the create event and setting invulnerable to be um say 120 so for 120 frames uh two seconds as it were at 60. run the game you can see we're kind of flickering now for three frames every eight now you can play with these numbers um put it however you like you could do it um every four frames less than two being much faster much faster flicker uh and so on i just i just liked eight and two those were the numbers i liked um you could do whatever you want um you don't need to do it less than two it could just be if moddy you get the idea so before we move on that's uh just set and vulnerable back to zero and now we need to actually make the function that hurts our player now that we have the functionality for invulnerable and so on um so i'm going to minimize objects here go to scripts and i'm going to make a new script in theory we should start like collating our functions um rather than having each function b1 script uh but i figure for now we may as well keep to the workflow we've used so far to sort of avoid confusion um so we're just going to call this script her player and that's just going to contain the function player all right we can get rid of this default uh thing up here i should probably get rid of that in preferences but for now it's fine now let's make our font nice and big okay we want three arguments for this function that are going to be a direction the direction we've been hit from um the amount of force we've been hit with which determines how far back we're gonna get knocked and uh lastly and probably not least uh the amount of damage we actually want to deal let's uh put that on the next line i'm just going to write the function and then i'm going to talk you through it all right so the first thing we want to know if we're going to hurt our player is is the player invulnerable because if they are then we don't need to do anything else because they're invulnerable then we want to reduce our player's health by however much damage that we're supposed to take so when we don't want it to drop below zero so we'll use a max function just to put it to whichever is higher zero or their health subtracting the damage that's supposed to take next up we want to check to make sure our player's health is still greater than zero and if it's not then we'll come down to this else and we'll do whatever we need to do to kill the player that's probably going to come in the next part but assuming they still have some health left uh we want to do a knockback effect all right luckily we already have a state designed entirely for this we've got players debunked for when we roll into walls and we get to reuse that to do exactly the same thing um uh when we get hit by an enemy okay i'll just middle click that just to bring us to it so you can remember how that works okay we we set like a speed and a move distance and uh set our direction up correctly and we get knocked back using that um little hurt sprite okay like this one okay so back over in our her function so we set ourselves to the the bunk state we set our direction to be whatever the direction of the force is minus 180 that's because if you remember bonk actually moves you backwards from your direction okay so we kind of want to face the direction that uh we uh we are being knocked in um you know whatever enemy hits us is going to know the direction from the enemy to the player um so we're going to take that direction and then reverse it so the player is facing towards the enemy um so they get knocked away from the force all right move distance remaining is simply set to force whatever variable we passed in here so that's how we determine how far we get knocked back we do a little screen shake since we have the function to do that nice little touch just adds a little bit of gravitas to getting hit uh flash will set to 0.7 even though we don't have the functionality to actually make the player flash yet that'll come in a little bit and we're setting vulnerable to 60. so they can't be hit again for the next uh 60 frames now all we have to do is make it so we actually use this function all right so let's go to our p enemy object because i i think for the vast majority of our enemies we're going to want to make it so if they touch the player that the player gets hurt you might want to only do that for some enemies or do checks to make sure they're a touch damage style enemy or whatever it is but for now we're going to keep this simple so every type of enemy we're going to have is going to hurt the player on touch so with that in mind p enemy we want to go to a collision event add a collision event with oh player and if that were to happen we want to do hurt player open bracket and you can see at the bottom here we've got the things we need to pass in the direction the force and the damage all right the direction is simply going to be point direction x y so wherever this enemy is and o player dot player.x oh player dot y um let's make this big so we can actually see what we're doing um so that's just going to get the direction between the enemy and the player which is what we want that's the direction uh the force is going to be enemy force touch and the damage is going to be enemy um damage touch all right which are not variables that currently exist uh but we'll make them now okay so let's come back to our workspace now uh still with our p enemy object go to variable definitions scroll right to the bottom here we're just going to add a couple of new variables i'll zoom right in here we're just going to name them what we just named them in the uh the collision event okay so enemy force touch uh is going to be 32 all right uh by default so 32 pixels like a tile or two tiles i don't even remember um an enemy uh damage touch which by default uh we're going to set to 0.5 or half a heart okay and then you could go into uh oh slime if you wanted and come down to oops scroll down to the bottom here and we could set those to be different so like a slime could deal two damage or whatever um but i think those defaults are pretty good so that's now we can see that in action now i think so let's press f5 from the game just run over to our slime it's gonna headbutt us for half a half damage each time i think our health is still set to something weird which is why it's sort of uh maybe a little tricky to see what what's going on there where do we sell health oh game i think it was yeah so our health is 3.92 so let's just set that to 3.0 and the health max to 3.0 as well um just so it's really clear run into our enemy head but half a heart each time you can see when we are flickering and vulnerable we can run straight through the enemy and don't take any damage if it would kill us you'll see nothing happens because it's expecting to send us to a dead state which we'll come to next time okay lastly we're going to add the the red flash really straightforward to do works basically the same way as our white flash did i'm going to duplicate our white flash shader and call it sh red flash come to the fragment shader it's the second one on the little tabs and here where we added flash to all of these different values what we're going to do is uh still add it to red but from our green and blue we're going to subtract it all right um that basically turns this into a red flash shader all right we can then close that and open up our player object again i'm just going to press ctrl t type o player so i just don't have to scroll to find it go to the draw event and in here when we would actually want to draw the player we're going to say if flash does not equal zero shader underscore set flash shader u flash is going to equal the shader get uniform flash shader flash you'll remember when we did flashes for other things with the white flash we actually did the uniform getting in the um the create event but since we don't know which shader we're going to be using here we'll do we just have to do it after we start the shader and then shader set uniform f u flash flash okay so it takes our flash variable and sends it across to the shader then underneath our drawer we want to say if shader current open bracket close bracket does not equal minus one that means we've got a shader set shader reset okay no need to call that if we don't actually have to um we just call it if we happen to have set a shader of any description leaves us flexible to set other shaders and things instead if we want now at the moment this will make us flash white um and i can show that super super quick we run the game run into him yeah because our shader is currently set to sh white flash all right so we're getting a white flash when we get hit by the enemy maybe you like that but i like to think you know when we're getting hit it's kind of a negative thing and a good way to reinforce that is with a red flash just from a design perspective really um so in order to make it use the red flash instead when we go to hurt player uh and we're doing the with player stuff down here i'm just including here uh flash shader equals sh red flash all right simple as that run the game when we get hit by this guy now we flash red and we take half heart damage and we become vulnerable all right thanks for watching guys um as you can see it really doesn't take we didn't write a whole lot of lines of code this episode and it doesn't really take much to get a really nice sort of polished um like on-hit effect like this it is really worth doing so uh make sure you do take the time to add this sort of thing um to your games get those little invulnerability flash get the the red on hit flash you can you the player gets plenty of feedback and they know what's going on all right hope you enjoyed this one we'll get the player to actually die next time um and i'll see you then a huge shout out to my patreon supporters without whom i couldn't do any of this work and a big shout out in particular and no particular order to the following cool kids of which there are way too many we're going to try and speedrun these names tranquil leonardo faraz elizabeth and landon brown gage hunter the game guru julian cropley darnell braxton michael kolich john keane i stephen jenninger borgia mk ultra it's matt paul hello winter rachel stewart arctics feral princess john c team d jordan hank dalvore vacants phil keane pong pong zhao jason welch andrew gilbert riva keizer ho boris the wizard zach culler figgy cabbage pants euron peter leo scott matthews mark burgess myani legoglow rene dam rupinder hair darkrider0318 jason relentless rex bertie tt dacadon to go robert churchill jonathan basil the dog scotty wing and max m thank you all very much for your support thank you for watching and i'll catch you all next time
Info
Channel: Shaun Spalding
Views: 6,512
Rating: undefined out of 5
Keywords: Game Maker (Video Game Engine), Tutorial, GameMaker Tutorial, GameMaker, Game Development, Indie Games, Tutorial Series, Game Maker Studio, Making Games, How to make games, GameMaker Studio 2, GMS, GMS2
Id: WaWdZ6DW9M4
Channel Id: undefined
Length: 17min 1sec (1021 seconds)
Published: Fri Dec 11 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.