Player and Enemy Health (GameMaker Studio 2 Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey you guys Craig here and welcome to a new game maker studio 2 series this one is going to be on combat and it's gonna be broken up into a few pieces this first one we're gonna talk about enemy objects and health bars so health for both the player and for enemies let's jump right into it this is gonna be a pretty quick one the first thing we want to do is make an enemy sprite I'm gonna just make a red circle it'll be the evil counterpart to our blue player and call that spray enemy and we'll go ahead and make an object as well obj enemy give it that sprite and then the next thing we're gonna do is set up our enemy health so we're gonna use two variables here one to define its max health and one to define its current health and this is useful for drawing health bars it's also useful if your characters are able to regenerate health so they don't go over there max yeah just a lot of good reasons to have it so we're gonna have both so max HP you can make this whatever you want it's configurable you know if you have different enemy objects that can have different max HP's you could increase it through levels or items or whatever you know up to you and then we'll have current HP and that will start at max HP so full health to begin with the next thing we're gonna do and you guys can decide whether you want this to be on or off you know you could turn it on for debugging or something but we're gonna draw a health bar and we're gonna put this right over the enemies head the first thing we wanted to do is remember to draw ourself very common bug we forget to draw self the second thing we want to do is draw health bar and this is going to take quite a few arguments there's the top corner and the bottom corner so that's x1 y1 and then x2 y2 there's the amount which I believe is a percentage the current health over the max health times 100 and then it asks you for colors and rection and whether to show the background whether to show the border so we're just gonna fill that all in you guys can play with it tweak it put whatever you'd like in there the first X will just be our enemy's X so that's the left hand side the first Y it's going to be Y minus some small offset to put it above the head maybe like eight scooch it up eight pixels the second X will be X plus our sprite width so the health bar will be the same width as our sprite and the second Y will be Y minus two or something we'll see if it's too big we can change that the amount this is where we do our little math to get a percentage it's going to be 100 times front HP / max HP so as your current HP goes down that's gonna you know be a small and small value the background color will just make it black so that's if there's no health what clothes should be the min color so what color does it turn as it goes down it's gonna be red of course always red and the max color the color when it's full definitely green very classic the direction I believe we might have to hit help on this one and our options are ah so this is actually numbers where it's anchored is what they say you say zero it's left so it's gonna drop down like this if you say one it's right it's gonna drop like this and probably my camera's backwards actually so you know you just play with it you'll see you can also have health bars that decrease or increase this way we're just gonna put zero it's anchored on the left the show back is whether or not to include that black background we'll put true just so it kind of sticks out and then show border you can wrap it in I think a black border mmm we'll put false these things are very configurable you guys can choose whatever you want for these you can also choose whatever you want for the beats the only thing that actually has to be this is that value right there so there is our enemy just got some health it's got a little health bar so we can keep our eye on it and then we're actually gonna go ahead and do the same thing for our player let's jump back into our enemy real quick we could make a common parent for our player and our enemy a parent that's like you know has health obj has health or whatever and they both inherit from that you guys could do that if you wanted to so the way that would look is you would take this code right here and you'd you know make another object and you'd move this code into here and then you know set this as a parent like that the reason I don't want to do that is because it ties our enemy and our player together pretty tight so you know they're inheriting some common functionality but in reality our enemy and our player are not gonna work that similarly probably a better idea is to make some common scripts that they both use and actually we might do that here so we'll say knit health and we'll move these values in here it will actually make it take the max health as a parameter so you can use this little label thing here to have it show up when you're typing you can see what your parameters are so this will be instead of 10 will say argument 0 so whatever gets passed into this script is what the max health is gonna be and then our enemy we can use that by saying a knit health and the max HB will be 10 not one there we go 10 and then our player in the create we can do the same thing you guys can ignore this extra code here this is from some of the other tutorials that I've done this is from the inventory tutorials you guys can check those out you are interested in that but yeah so there's initializing your health variables and then this drawing will keep draw self here we'll keep that separate from the health but we could make a script draw health and plug that in and we don't need to take any parameters here it's gonna look this up off of the calling object so as long as the calling object has called in it health at some point draw health will just work so draw health and not draw a health bar that's a built-in function we want to use our script that we called draw health and for our player we're gonna plug that in as well now if we pop some enemies in here and we hit play we should see that we all have health bars yeah so next time if you guys are interested we can talk about attacking we'll start with melee attacks and we'll actually put these health bars to use as they'll be going down when we beat these enemies into the dirt so there you go that's a pretty simple way to set up your enemy health and your player health scripts are pretty powerful things it's nice for code reuse so yeah let me know what you guys thought and definitely stick around for the next one we'll talk about attacking so thank you guys see that
Info
Channel: Kraig McFadden
Views: 1,944
Rating: undefined out of 5
Keywords: software, code, kraig, mcfadden, gamemaker, game, maker, studio, tutorial
Id: L1bz3Mg11tU
Channel Id: undefined
Length: 8min 12sec (492 seconds)
Published: Thu Jun 04 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.