Construct 2: Basic Enemy AI & Enemy Specific Health Bars | Jerementor

how's it going everyone I am Jeremy Alexander and welcome to another tutorial in this tutorial we're gonna be making multiple enemy health bars which means we're gonna have to make some very basic enemy AI so let's start by taking a tiled background I got this from the retro style project by default let's add a solid behavior to it which I already have done then I'm gonna kind of just design around the window size here since there's not going to be any object no player that has a camera attached to it no scroll to behavior I'm just gonna use the window size because that's what our project by default will kind of position us to so let me just kind of make this and what this is is a copy of all these tiled backgrounds with the solid behavior but this is gonna create walls for our enemy to kind of land on the floor and then bounce off these walls and flip directions now this is very very basic and I think in the future we will go over more complicated AI but for right now this is getting the AI finished so then we can actually make multiple health bars so here's how we're gonna do this we're gonna make a new sprite we're gonna make it 16 by 16 and we're gonna color this anything but red because the red is gonna be the color of the health bar and let's give it some eyes now when we give it detail or when we give it anything more than just a solid color we're setting the orientation of this and therefore it's kind of very important that you do that at least I think it is because this way you actually know what way your enemy is facing and from there you can just make it it's a lot easier to program otherwise if you would if you just had a blank color you wouldn't really know let's call this our object enemy and let's add the platform behavior to it now I know you're probably thinking why would we add that it's the enemy we're gonna turn default controls off and we're gonna turn the speed down to a hundred and kind of that's the thing with constitu you can add behaviors to things that you wouldn't have imagined in other programs like you wouldn't have imagined that the platform behavior which sounds like it's for the player can work really really well for the enemy as well so there's some cool things like that and some fun tricks to mess around with but we're gonna add the platform behavior to this and we are pretty much done we just need to add two instance variables we need to add a direction variable and we need to add in a health variable this health variable is going to be just like our player health bar which is where I think this tutorial kind of spawns from I did a how to make a player draining health bar this is gonna be an enemy draining health bar we're gonna set this to 32 because that's what we're going to make our enemy health bar with and just like in our player health bar video we're gonna tie the enemy's health to the enemy health bar width so let's do that let's double click and make a new sprite let's make this 32 by 8 and let's make it red now just like in the player health bar video we're gonna take the origin point and we're gonna assign it to the left and that way it's going to drain down from the right so it's X out of this let's call this our object e health bar and now we're ready to go we have our enemy it has our instance variables and we have its platform behavior and we have our health bar cool so let's go to our event sheet here and let's program our enemy what we want to do is we want to say mmm let's go add the event to our enemy on created and when the enemy is created in this case we are going to be creating multiple enemies so this will always be applied to it since there's already an enemy here it should actually still trigger this event and what we want to do is we want to set the direction variable so let's go into our enemy set the value of direction to choose between two random numbers I'm going to pick negative 1 and 1 so now it's got two numbers to pick from every single time an enemy is created and since there's already one here it'll randomize between negative 1 and 1 now what we can do for our very basic AI is we can compare that we can pair the direction to see if it equals 1 or C if it equals negative on which one does it choose now if it chooses 1 we're gonna add the action to the enemy to the platform to simulate control moving to the left and because we're already facing to the right we are going to set this mirrored 2 we're gonna set it to mirrored because when you're going left you want it to flip since we're already facing from the right let's copy and paste these for Direction equals negative 1 and let's flip them so now Direction equals negative 1 equals not mirrored and pressing right so now we're almost done with our eye we have one more thing to do if we hit play on this and actually let me make sure that this is running on nwj as it is if we didn't play on this it should fall and there we go but we can see here that it's gonna stop right at the wall so let's actually first let's move this up just a little bit here and let's actually program to detect if the enemy is by a wall now fortunately because we're using the platform behavior it's very easy we're gonna add in an event for the enemy if we are by wall to the left and let's copy and paste this let's copy our on created to action where we're setting the direction instead of choosing we're just going to set this since it's to the left we want to move it to the right and since simulating right is equal to negative 1 Direction equals negative 1 so let's just copy and paste this if we have a wall to the left then we want direction to equal 1 which means that we are going to the left cool we're now this needs to be right I was like wait a second ok so now we have our very basic enemy AI and we can group it as such so let's do that and what we want to do now and I can hit play here just so you can see this so it's gonna land negative 1 bounce off bounce off and there we go so now we kind of have our very very simple AI and we're gonna add in the functionality that we did for our player where if we click on our enemy the health bar will go down but really what we want to get that get ahold of is when we have more than one enemy how do we create health bars for every single enemy and how do they stand out stand alone essentially how are they how do they act independently that's what I was trying to say so the first thing I think we're gonna do is this now we're gonna pick a spot or a coordinate here one 45 by 75 is what I'm gonna go with and we're going to say this system every X amount of seconds so every 4 seconds we're going to system create an object object enemy on layer 0 at oh I already forget what I said let's go back and check another coordinate 156 by 80 that'll work kind of round it off 155 by 80 so it's going to create it pretty close to the middle of our level here and what we want to do is we want to create a for each loop now this loop is going to repeat its actions for every single instance we create so that's really the important concept to get a hold of from this tutorial let's do this let's add the event system for each loop and just like it says there the actions will repeat once for each instance of this object so for every single enemy that's created this will this will be run and what we want to do is we want to make a sub event by hitting B and we want to say system trigger once and we want to actually create our health bar for every single instance that is created so let's system create objects we want to create our health bar on the same layer and we want to create an object enemy X and object enemy dot Y so this is probably the code that you were missing if you were looking for something like this we need to make sure that this runs for every single instance that's created and then we need to make sure that we trigger this once because otherwise it'll trigger it an infinite amount of times we don't want that and then we need to place it on the player but this is gonna kind of place it in the middle so let's actually you know let's just hit play and let's watch this work so when it creates okay you can kind of see already that it's kind of messing up here and it doesn't look good so that's okay because we're gonna come back and fix that but what we what the reason why this is happening is it's because it's triggering it but it's not actually running every tick so let's add an event system every tick let's actually set the position now of the health bar so let's type in set position and we want to take object enemy X an object enemy dot Y but this time we you actually want to we want to position it on top of our enemies so we're gonna subtract 15 from our x position and subtract 15 pixels from our Y position then we might as well since we're here in our every tick just like we did for our player health bar we want to set the width of our enemy health bar to equal object enemy dot health just like that all right cool so let's hit play and let's see if that bug is fixed and also what I can do here is since we're creating both of these objects we can actually put these off the screen all together so let's hit play now we're gonna have to wait 4 seconds for this to kind of pop up here but it should pop up and there we go now we have our first enemy we have to wait another 4 seconds and there's our second enemy now because we set the position we don't get that bug anymore but because we have that for each loop every single enemy that is created is going to come with a new health bar okay cool so because of that how would we actually destroy our enemy how would we destroy our health bar here's what we're going to do we're going to add an event for the mouse and we're gonna say on object clicked if we click our player then this is where we're going to subtract from the health we're gonna go into our enemy we're gonna not set the value we're gonna subtract from our health now we set it to 32 now this is where you can kind of get into your own health system whatever your health bar system is maybe it's hard maybe it's a bar like this or maybe it's just something completely new this is where you want to define how many damages lost and I'm sure there's a more complicated way that you're trying to do it I know that I do a bunch of different ways for different bullet effects that hit the player or hit the enemy and how much damage they do you might want to do percentages there's so many different things you can do we know that we have 32 hit points essentially we set our health to 32 we set the width of our health bar to 32 you we're gonna drain 32 pixels so we don't want to click 32 times if I put this to one it'll subtract and I could have to keep on clicking until it gets to 32 or sorry until it gets to zero so I could kind of cut it in half which i think is what I'm gonna do just to speed things up here but you can also make it a little bit less depends on what you want to do but anyway that's the logic now if our health is equal less than or equal to zero here's what we do we're gonna compare that if our health is less than or equal to zero generally we're gonna object enemy destroy the enemy now when we destroy the enemy that's all fine because the health bars width is already at zero but it's still there that object is still there so we actually need to put that in the for each loop as well so let's hit B on our for each loop and this is actually pretty cool what we're going to do is we're gonna compare the width of our health bar and we're gonna actually type in compare with and we're gonna find out if it is less than or equal to zero just in case it goes over and if that is the case then we're going to destroy our health bar so there we go that is how we would program this so let's hit play and you should see in four seconds here an enemy will come onto the screen just like that we have our AI running back and forth and if I click on it it lost 16 health and if I click again they're both destroyed so because we have that for each loop it'll target every single instance and it'll actually single it out rather than having it act as a whole group because these are duplicates of the same object but because they're instances were able to actually pick them out and program for them individually as if they were individuals essentially as if they were just independent objects which is really really cool and really really powerful so I hope that this has kind of made this a lot easier for you kind of showed off some basic AI and we also showed off just an easier way to bring in for each logic into your brain so hopefully this has helped a lot thank you so much for watching this video and as of this video I had a thousand subscribers on YouTube so that's really awesome as well and thank you for helping me there I really just can't see where I really can't wait to see where this channel goes so thank you so much for everything subscribing to this channel if you have any ideas for future tutorials please send me a tweet at general Mann tour or comment below if you did like this video please share it and give a thumbs up that'd be really cool thank you so much for watching again I'm Jeremy Alexander and I'll see you next time

16 thoughts on “Construct 2: Basic Enemy AI & Enemy Specific Health Bars | Jerementor

  1. 100th like. and this tutorial is helpful and hope it works on construct classic version. ๐Ÿ˜‰

  2. hi jerementor, so i try to create enemy with their own health, and i create creatable player, but when i got 2 same player hitting the same enemy, the health is only reduced by 5, it should be 10 because 1 player equal 5. what should i do?

  3. I wrote the code correctly 3 times in different projects, and it always happens the same, The life bar is created endless times causing the game to go slow. Any more code for this to stop happening?

  4. great video. trying to figure out how to make a conpanion character follow the player but attack an enemy when the enemy is within a certain range of the companion. then when the enemy is gone the companion goes back to following the player. it may be good to also do a quick video on hot some math formulas are set up and used in construct. example = distance formula or angle formula. etc…. thank you

  5. Have you done anything on arrays in Construct 2? I've found that if you select a cell outside the established range of the array it'll still return a value of 0. I had to change my default value for arrays to -1. Just sharing. Great vid, man

  6. Nice and clean code!
    Great timing too as I need to work on Enemy AI for my current game project!
    Keep up the great work and congratulations on hitting the 1K subs milestone! ๐Ÿ™‚

  7. I see why you had to make a video… its a bit complex to write out and showing it a video this way was WAY faster.. again TY… I will work to get this park into my code and see how it works in the ongoing test game Im working on.. Lots to learn…

    Gratz on 1000 Subs MAN!! So cool..

Leave a Reply

Your email address will not be published. Required fields are marked *