Create a health bar in GameMaker studio 2 DND

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay for this tutorial we're gonna make a health bar so we have a basic game setup where i have some enemies going up and down here and if my player were to hit one then we'd need some form of health notification if the player can get through all of them and hit the detonator then we can escape and we can go through the crack tools excellent okay so let's get to it let's open our player so by double clicking on it and we're going to add in a create event and inside this create event we're going to add a variable which is going to be called hp and hp is going to be for health points and we're going to assign hp a value of 10. now i'm going to add in another assigned variable by clicking the plus and we're going to say hp max so this is working out my maximum health points and i'm also going to set that up to 10. the reason we do that is later on when we add the health bar in we're going to work out the percentage so i need to know the maximum and i need to know my current so this one here hp which is set to 10 we're going to take a point of this every time we have a collision so that's the basis of it let's now add in a collision event so i'm going to click on that event here and it's going to be a collision with the object enemy and you can see i've got some code in here already i'm just going to remove these and then i'll talk you through what it is we need to do okay so the first thing we add in is this here which is a sine variable and we're gonna call a hp variable and we're gonna say minus one relative now what that does is it takes one point off every time uh there's a contact between my player and my enemy right okay so to see this in action what i'm gonna do i'm gonna add in a spread to the bottom it's a debug event so scroll all the way down to this here and i'm just gonna basically get it to output the hp right so it's basically outputting the current health point so if we click the play um what you need to do is keep an eye on this output here and if i run towards this nasty looking player and we hit it oh there are now you can see there is a bit of a problem the problem is here that every time it comes into contact my play is not moving and obviously the the number is going smaller or smaller smaller is going into the negative figures so you can see our code is working exactly as it's supposed to um but really what we should do is move our player over here or we could make the transfer that player sort of disabled for a bit or whatever but the easiest thing i'm going to do is just move my player over to the side now in order to do that i'm going to use a variable so i'm going to scroll back up and i'm going to add one of these things in these assigned variable blocks i'm going to say x and then what we're going to do is x minus 200 so this is going to take 200 off the current position of x so it's going to move it all the way over so it's going to take 200 pixels off so if i just bash into one per second bang you can see that's now put me back but there and let's just do the same over here bang it's so you can see this is working now obviously if we hit the third one here it's going back sorry this middle one i meant no third one if we hit that one it's going back um you see it's it's obvious in 200 pixels back from what that is so you could if you wanted to increase it depending on what you want to do i just wanted to move mine back a little bit now you will notice at the bottom here they'll put this say zero now the downside is with this we haven't got any mechanism to sort of end the program so what we're gonna do is add in a step event now a step event will sort of run every step of the game and we can do a check so if i add in a step and then we'll just say step and let's add in an if statement which is here on it so i want to say if the hp is equal to zero then we need to do something so what i'm going to do is i'm going to restart the game so i'm going to scroll all the way down the bottom and we're going to go to this one here we start game now that way if our player what should happen now is when my health points go down to zero my players should come back to this location which is where the game starts so there are that's one two three here we go let's now just try and lose as many points as possible you can see it's it's a good luck okay last one and you can see there the whole game then reset and my play went down to the bottom so that works well here so the last thing we need to do now is get some form of health points up now we can do this in a few ways i could if i wanted to instead of using a bar i could use a draw gui event and add in a number on the top here so we are going to use health box for this tutorial i've said i want to use health bar so let's click on add and then let's go down to draw and then draw gui which is here okay now quite handily scrolling down you'll see in the sort of brownish here in the drawing you do have an option you have to draw in a health bar now if i just hover over it you can see it says draw a health bar from a 0 to 100 value in other words the values always got to be out of 100 so we are going to use a little formula so i'm going to say hp divided by hp max and then what i'm going to do is i'm going to multiply that by 100 so what this will do is it'll work at a percentage for me now i want these to all be relative to my player so that when my player moves it'll follow the player around and we're gonna set a size now in terms of sizing you're gonna have to play around with this because depending on how big or small your player is will depend on how big or small you want your uh bar so i'm gonna put zero minus 20 uh we'll go from the top there was try 40 and then from the bottom we'll also try in fact no i'll probably be negative 40. from the bottom then we'll do -20 and then from the right let's go for 20 as well okay now we need a background an outline and a main and a max color now my background in my game if you look is black so ideally i want something that's going to stand out but not too much so i'm going to use a light dish gray as my background the outline i can leave to white that's fine my min color i'm going to change so my minimum color is going to be like a red so i'm going to click on my red here and let's go all the way here and let's choose like a darkish color there you are click ok and then let's click the max color the max color needs to be green so i'm going to click on the green here and then we're going to go with a slightly darker shade there or something like that and then i'm going to click okay so let's see now what that is like now bearing in mind i might have to change these variables if i'm not happy so you can see this health pass there but it's not large enough really so we need to make a few changes so let's change this from right uh let's make our say 40 to see it's one good let's see now what that does hey well that's a bit better so it's made that now a little bit bigger we can probably go a little bit more as well so let's double up to 80 and let's see you know what our health bar looks like there well that's okay that's fine now probably that is a little bit too big for the size of my player but we know to scale it down if we need to let's see if it actually works so let's bump into one of these okay so just right there now which is brilliant you can see one point has come away and you can see the gray is there's the gray now we're showing that obviously we've lost some points and then every time we hit we're obviously losing more and i don't know if you've just seen that now which is lovely it's gone from a green to like an orangey type color so there's a bit of a blend going on here and then there are now we're in the red oh now we're really we're at that sort of red here because i think we got one point left and there you are the game that was reset because we've gone back into uh zero health points so we've now started again so that's really in a nutshell how you create a little health bar with the player and assign it upon school
Info
Channel: A bit of everything
Views: 218
Rating: undefined out of 5
Keywords:
Id: 1fp-sHx2-L0
Channel Id: undefined
Length: 8min 43sec (523 seconds)
Published: Sat Oct 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.