Gameplay Ability System in 40 Minutes -UE4 C++ Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] this video is sponsored by my course the unreal engine c-plus plus survival course using my knowledge from working in the industry we start from the basics and work our way up until we've created an online survival game with c plus we create vehicles clothing weapons steam matchmaking and much more get lifetime access for 25 dollars using the link in the description all right game playability system let's talk about it game playability system is used extensively in fortnite and is a really powerful system that has been in the engine for years no one knew how it worked and now everyone's using it and for good reason it's really really powerful let me try to explain what it does in like two minutes so i'm going to jump into this project here i'll leave this in the description for anyone that wants to try it out you can see down at the bottom of the screen here i've got three bars and the bars are displaying something called attributes so that's the first part of the system is attributes attributes can be health mana stamina ammo gold literally anything that your game needs can be like an attribute right the next part of the system is effects and effects modify attributes when i hit someone i might apply a damage effect to them that would lower their health attribute in fact check it out when i sprint the sprint ability uses a effect to drain my stamina right so we've got attributes we've got effects the next part of the system is abilities and the word ability can be a little bit confusing because literally anything can be an ability sprinting in this game is an ability there's this cool meteor ability we can do shooting that's an ability in this project so it's really up to you what you want to be an ability but ability is a kind of this high level way of just managing a bit of logic right here here's this meteor you can see there's a lot of different parts to it right there's getting into the screen my camera changes i spawn the meteor it hits someone that deals damage there's a lot of moving parts to that the great thing about gas is that it puts that entire process in one place so in a traditional game this meteor ability would be like sitting timers spawning all these different actors it would be so complicated i don't know maybe even hundreds of lines of code and this project with gas the meteor ability the entire thing is contained inside of one handy nifty little blueprint here it is here and this is everything this is the entire meteor ability right we change the location of the camera we wait for you to select where you want to drop the meteor uh we commit we use some mana up or whatever the cost is uh we wait for someone to get hurt and then we deal some damage to them and stuff like that so the ability system is really good at making you put abilities these big chunks of logic into one neat little package it makes your code quite easy to manage as well and there's also this concept of abilities are not linear like if you have a look at this ability here there's a lot of different things that can happen during the process like you can see here wait until player chooses a location to drop the meteor well maybe i'll choose a location to drop the meteor but i can also cancel the ability so the ability system recognizes that there are lots of different outcomes from activating an ability and also it doesn't happen instantly it can happen in multiple different steps and you'll notice these nodes with the little clock in the top these appear over and over and over again these are called async blueprint tasks and they're basically these nodes that they don't happen straight away right it'll sit on this node for quite a while and then when i finally choose where i want to drop the meteor then it's going to call this blueprint pin here so we use this all the time in abilities because abilities by the very nature are not instantaneous there's lots of different steps lots of different outcomes so that's the the three big parts attributes effects get modified by attributes and then abilities and there's some other things that we'll get into but those are the big ones and today we're going to be making a project pretty simple we're going to create a character we're going to give him some attributes like health attack power and then we're going to implement a punch ability which will deal damage to someone else okay so i'm going to leave a link to the starter project just download this make sure you have unreal engine 4.25 and then once you've unzipped it just click on generate project files and once that's done just open up this visual studio solution and then we can begin once you've closed the editor open up your gas.uproject file and just ensure that you have gameplay abilities enabled down the bottom there and after you've done that you'll want to go to gas.build.cs and underneath public dependency module names we're just going to add some private dependency modules um gameplay abilities depends on the gameplay tags and gameplay tasks and stuff so we just need to add them down here so you want to put private dependency module names and we're going to include these three modules in the project now we're going to hit local windows debugger and we're going to add a whole bunch of classes all right so in the project go to c plus plus classes we're going to add a new c plus class and you'll want to search for ability system component this is basically how we talk to the ability system anything can have an ability system component so if you wanted your weapon to have its own set of attributes and effects and whatever you could give it an ability system component you could give yourself one you could give ai people one if ai need to have abilities as well so it's fully up to you so i'm calling mine gas ability system component and i'd recommend calling your stuff the same as mine and then click on create class the next thing that we need is an attribute set because we're going to add health and stamina and all that stuff so add another c plus plus class and just search for attribute sit click on that and we'll just call as gas attribute set and finally we're going to add our own custom ability class because we want to add a little bit of customization so we're going to do game play ability and just find game playability click on that one and we'll just call this gas gameplay ability and then create that class too and now we're done okay so the first thing that we need to use gas there's a little bit of setup a little bit of boilerplate and it's kind of unavoidable so the first thing we need is our ability inputs so abilities are usually bound to input i might bounce the left mouse button to punch that's my punch ability i might bind the lift shift to the sprint ability and so on so we're going to add a new class and we'll call it gas ability input id the first one should usually be none and then there's a confirm and cancel one that uh a part of the system and then we're adding our own punch one here at the end so define that and then we can move on okay so now it's time to add attributes like health stamina and attack power to the game so you do that inside of an attribute set you can share attribute sets so maybe there's a weapon attribute set that just the weapon needs maybe uh i need mana but a i don't so i give myself the mana attribute so you can you can do whatever you want it's quite common though to have all of your attributes just in one attribute set because it makes things a bit more simple so what we'll do is we'll add these nice little helper macros these are from attributeset.h so if you go in here you'll find them in there somewhere but we're just adding those there's a kind of convenience they add getters and setters and do some stuff for you so worth adding those there the next thing we'll do is add a constructor we're then going to add a get lifetime replicated props because all of the attributes are going to be replicated when my health changes i want other players in the game to know about that so we need to replicate them and we'll add our first attribute which is going to be health and there's quite a bit of boilerplate here so don't be too alarmed but basically what we're doing is we're defining a health attribute we're using that handy macro to add some getters and setters and stuff instead of having to do that ourselves and then we're adding an on rep function as well down there so we've added health let's add stamina and attack power as well so there's our health there's our stamina and these are attack power so those are the three attributes that we'll add what you want to do now is create an implementation for all of those functions so add your constructor get lifetime replicated props and then your health stamina and attack power gas requires that we tell it whenever an attribute changes it's all part of the system so just add the gameplay attribute rep notify inside of your on reps and that's all you need to do inside of get lifetime replicated props we just need to replicate them we're using a condition that says always rip notify there's a basically rip on reps will only call if the value changes but we want it to just always change so that's what we're doing there you will need to add the net slash umro network header to this file and i think you need to add the ability system component as well so add that and that is how we define attributes and we'll give the attributes to our player in a second but the first thing is just getting them created so depending on your game some games might want to tie each ability to an input some games might activate abilities completely differently but in our game we're just going to map each ability to some input so you click the leaf mouse button and it activates the punch ability so what we'll do is inside of our gas game playability we're just going to add a ability input id that's the input that should activate this ability so we'll create a constructor and i think i'm just going to leave that constructor empty actually yep yeah we'll just leave that empty and you will have to add the gas header file here as well so i've got my character how do i give him health stamina etc the first thing you want to do is add the public eye ability system interface and you will also need to add the ability system interface header file and we're also going to add gameplay effect types as well i've already got a couple of components on this character so all i need to do is add the ability system component along with all the other components and i'll also add my attributes to the player so i just put my attributes down here like that next up you have to implement this function it's called getabilitysystem component and uh the ability system uses this function so you have to implement it and all you have to do is just give it the ability system component that you added next up just come into the constructor here and add a ability system component make sure that we set it as replicated and then there are a couple of replication modes um minimal mixed and full minimal will replicate not much stuff to other players mix will replicate gameplay effect information and uh full will replicate absolutely everything generally you want to use either minimal or mixed if you're making an online game and if you're making a single player game then you can't get away with just using the full replication mode so that's how you add the ability system and then we're going to add our attributes by just creating a default sub-object and then just put our attributes in and now our player has attributes the next question is how do we give the player default values because it's going to have zero health zero stamina we want some default value like a hundred health as a default uh so how do we set that up the answer is to make a initialize attributes function and remember how i said at the start of the video that attributes are modified by effects this is no different for the default attributes that we give the player uh defaults being like 100 health 100 stamina whatever we need a gameplay effect to actually sit the default values so what we'll do is we'll come down here and we'll add this gameplay effect this is going to be our default attribute effect it's going to set all the default values for our attributes so we'll right click on initialize attributes create an implementation of it okay so we're about to learn how to apply effects to a character or an ability system component and this is really important this is a really big part of the system how do we change someone's attributes when i shoot someone how do i give them a damage effect that's going to lower their health attribute and so how you do that is through the ability system component what we're going to do inside of initialize attributes is we're going to make something called a context handle a context handle is just some information about the context in which we're applying the effect for example if i shoot someone the context is i'm the instigator of that shot and i'm damaging him so there's a bit of context around you know how you apply the effect the next thing you need to make is a effect spec and we do that the reason you make a spec is because effects can have different levels and lots of different things like that so we're going to use the default effect at level one we're not going to be bothering with levels in this video we'll just have one here and then we'll give it the context finally if a spit handle is valid we are going to apply the default values to your player using the effect the way we do that is we take your ability system component and we apply the gameplay effect to self there's another one called to target which would be used to feel like shooting someone or something like that but in this case we're just applying the default values to our self i also want to give the character the punch ability off the bet and in guess you actually have to give the ability to the person because i want the player to be able to punch by default i'm going to make a give abilities function that will give the player some default abilities so we're going to add an array of gas gameplay abilities and these are the default abilities to give to the player and we're just going to put punch in there so we'll give that and we're going to create an implementation and i'll show you how to give abilities the server should always give abilities so the first thing we do is we're going to check that we are the server by using has authority and then we're going to say is our ability system component valid the next thing we're going to do is just loop over those default abilities and to give them to the player what you want to do is take your ability system component and you just call this function called give ability you can see here's the ability spec you can define a level for the ability because abilities again can have levels as your character levels up abilities can get more powerful but we're not going to be touching that today and then you also need to give it the input id and we'll be setting that to the punch input in a little bit okay so there's a little bit more boilerplate code so we need to add a possessed buy and an on-rep player state and i'll tell you why we're adding these so persist buy as a function that gets called on the server and on rip player state is a function that gets called on the client you need to initialize the ability system on both the client and server so these two functions provide a good place to do that so we're going to add these two functions and always remember to call the super function because that can make bad stuff happen if you don't so super exist by new controller so what you need to do is you need to tell the ability system who the owner of the ability component is so what you do is you do a net ability actor info and you need to tell it who owns the ability system and then also who's the avatar and you can see here the owner owns the component and the avatar is the actual physical thing you can see in the world so maybe my controller owns the ability system component but my character is the avatar it's the thing you can actually see in the world um in my case i want the character to be both the owner and the avatar so i'm just going to do this and this and then we're going to initialize all my attributes to the default values and we also want to give the punch ability to the player so we're going to do that as well and let's just say server yes the client is a little bit different so it is still going to emit the actor info and initialize the attributes but only the server should give the abilities so the client doesn't need to do that one thing the client does need to do is bind the input to the abilities that doesn't just happen by default you actually need to set that up to happen so what we're going to do is we're going to say if we have a valid ability system component and input component the first thing we'll do is make some binds so here it is here so if game ability input binds confirm cancel we give it the name of our input in num and then a bit of magic will happen behind the scenes where it will look through and match our enum up to inputs later on and then we give it the confirm and the cancel values of erroneous so yeah once you've done that you just give the ability system the binds oh and you also need to give it your input component and there you have it i'm also going to copy this and inside of setup player input component i'm going to come down to the bottom here and i'm just going to paste it in there as well and this is because sometimes the ability system will be valid but the input won't be valid and one function will get called before the other and so by putting it in both functions we are guaranteed that the ability activation will get bound properly at the top of gas character.cpp you'll just want to add these four here to files here this is just our custom stuff and i'm not sure if we needed that one but add that anyway just to be safe okay finally we can start messing around with abilities and setting stuff up sorry that's really boring just boilerplate code and in fact if you make any project and you put gas in it you'll basically write that code almost every time so but yeah at this point if you've done everything correctly you better click on local windows debugger and now we can jump into the editor which is where we will make abilities and effects and things like that okay so it's time to add our first ability what we're going to do is add a new folder and i'll call it abilities and then inside of there before we do that let's make our default uh effect to set all of our attributes to default values so to do this make a new blueprint class and then search for gameplay effect click on that and then you'll want to type in g e for game play effect and then we'll say character defaults open that up and it's going to look a bit intimidating at first but i'll go through the things here so the first thing is the modifiers we actually want to just set modifiers so we're setting the default values if we click the drop down notice we have health stamina and attack power so we're going to set the health we're going to override it to be 100 by default we're going to set the stamina to be i'll just do 100 for that one as well and then by default i'll say that when you attack someone you actually deal let's just say 20 damage when you attack someone so now we've actually initialized our attributes so what we need to do is tell the character to use this gameplay effect on itself and that'll sit the default values so what we'll do is we'll open up the character here third person character and in the class defaults you can see default attribute is fit and just click on character defaults next up let's just make the punch ability while we're at it so um blueprint class we're gonna search for gas gameplay ability and then uh ga punch so that's our ability there and so we want it to be bound to the punch input so when i click the left mouse button we want to punch so we'll go to project settings we'll go to input action mappings and i'm going to add one here i'll just type in punch and then left mouse button and gas game playability system is going to automatically find this punch and then it's going to map it to that ability so if i open up ga punch there we go we won't make the ability just yet because i want to sort of show a few things first okay so to check if the ability systems work and go ahead and play in the game and then open the console with the tilde key and then you want to type show debug ability system and so what this will show you is on the left hand side there you should see health stamina and attack power and you'll see that those values are now initialized and now we can start sort of making abilities and doing different things but let me show you how gameplay effects work let's show you how to actually apply these let's make it so that we hurt the character when he starts playing and we'll bring his health down just to see if we can do it so i'm going to make a blueprint here make a gameplay effect click on that and i'll call this ge damage so we're going to harm the player and all we need to do is modify something so we're going to add a modifier we want to modify the health value and we're going to add negative 20 so we want to bring the health down by 20. i'm going to compile and save and it's actually quite easy to do so we'll open up third person character on begin play we're going to drag an ability system component and apply gameplay effect to self and the effect we want to do is damage we want to damage ourselves and try it out again i'm just going to put level 1 and don't worry about putting in an effect context because we don't really need one in this case and when we hit play i'll just run my command again and you can see that it did hurt the player you can see the player's health is now on 80. so now we're sort of understanding how to make attributes and how to modify them and stuff like that so now i think it's time to move on to our first ability which is going to be a punch ability okay super quick uh animation thing we have to do go to animations nmbp and then go to the enemygraph and just type slot and we have to do this so that we can play montages on the character so we'll compile save okay so let's go into ga punch so when you make an ability you have a few different events the main ones being activatability and on end ability so when this ability gets activated we want to start by playing the punch montage and when the punch montage gets to a certain point maybe like there then we check if you've hit the player then we deal the damage so there's a few different steps to the montage so when you activate the punch ability we're going to play a montage and to get our players skeletal mesh is actually super easy just type get actor info right click on this pin and split it and then just plug the skeletal mission and that's our player's skeletal mesh right there so all the important things you'd ever want to access right here and the ability for you to use so we'll play the uh attack montage and then when the montage completes let's just end the ability so let's make sure it actually plays the attack animation and we'll try it out one thing i forgot by the way go back into third person character and under default abilities we have to give the character the punch ability as one of the default ones and then compile and save and then let's try it out there you go so when i click the mouse button the character does a little attack animation but right now we're not checking if you hit someone we're certainly not applying damage or anything like that so that's the next step now so because we want to check for the um the punch when the punch reaches a certain point we need to use something called an anim notify anima notifiers are really simple and i'll show you how they work in a sec but let's just make it first so blueprint class nm notify click on anima notify and i'm going to call it a in underscore um i guess i'll just call it punch so we'll open up and notify punch go to functions receive notify so when the animation gets to a certain point it'll fire the notify then we can check if you punch someone then we can deal damage so that's how that's going to work so what we'll do is we'll get the owner of the mesh we will cast them to a third person character we will then tell our character to do the punch so let's open the character up we'll add a new custom event here and we'll call it handle punch so our anim notifier is going to tell the character to handle and then we'll just check this box here to say everything worked good we'll then open up our mannequin animations attack okay so when do we want to deal the damage it's about there right when the hand strikes the enemy then we want to deal some damage so under the notifiers area here just right click and add a notify and then add our punch notify okay so when we punch someone really all we want to do is we want to find an actor that was near our hand when we punched and then we can deal the damage to that actor uh so what we'll do is we'll use this handy little node called sphere overlap actors and that will give us all the actors within a given sphere so let's give it the location so we're going to take the mesh and we want to get the location of our hands to do that we just do hand ah like that we want to ignore ourselves because we don't want to be able to punch our own character that doesn't really make any sense so you want to do that and then we only want to look for pawns right if we hit a another player so go ahead and click on paul in there and third person character there you go so if we hit someone so if there was someone then what we want to do is we're going to use something called a gameplay event the gameplay event being we hit something right that's the event because gameplay abilities use a system called gameplay events as well they're pretty simple and i'll show you how they work so let's let's send a gameplay event to ourselves and let's say that we hit something right so the actor that we're telling the gameplay event about is the instigator the person throwing the punch and we are going to make gameplay event data i'm going to plug in the instigator here the target is the person that we hit so we're going to grab the person that we hit plug them in and then we're going to do ability target data from acta and plug that in the target data is used when we apply the damage to the targets we need to make some target data as well okay at this point we can compile and save let's now go back into ga punch and what we're going to do is after we play the montage of the punch then we're going to wait for a gameplay event so what i'll do here is i will drag out wait gameplay event and let's go back to the character and we need to tell it what event we're sending to it we don't we don't actually have any gameplay tags uh so let's make a gameplay attack we'll just call it weapon.hit right the weapon hits something let's add that tag and so the event that we're looking for is weapon.hit so if if any other event comes through we don't care about it we want to know if a weapon hits something we'll only trigger it once because that only makes sense we just want to apply the damage once and then if we hit something we want to apply the damage to them so the way that we do this is we're going to take the event data we're going to drag off from target data and we're going to apply a gameplay effect to target and we want to apply the damage to the target the person that we hit so there we go that should be pretty much it and i actually did something bad here this happens straight away we only want to do it when we receive the event so make sure you drag off that very important and then if the montage finishes and we don't hit anything then we can just end the ability right we don't care about that anymore so we'll just connect that up like so and it's always a good idea to clean things up as well so i'm going to promote this to a variable i'll just call this async task something like that and i'll show you how to clean this up at the end it's usually a good idea if you're using these async ones store them in a variable and then we can clean them up when we're done so under functions just go to on end ability take our async task and if it's still running if it's still valid then we need to clean that up otherwise it'll just sit around forever doing nothing so all you need to do is just do end task and that will clean it up so the next thing to talk about is how do we make effects like when i get hit how do i get the character to flinch up or when i shoot someone how do i get blood to come out of them that's called gameplay cues that's how we do visuals and game playability system so the way that you want to do this is we'll start by going to the gameplay queue editor and we will add a new gameplay tag and they have to have gameplay queue at the start it's very important i'm just going to call this gameplay cue dot took damage and then we'll add that now that we have a took damage tag we need to add a handler so just click add new and you can spawn an actor we don't want to do that we're just going to use a static notify so we'll click on that and then we just want to call it gc underscore took damage so there's our new gameplay queue right there right click on a hit react and create a montage out of it and we're going to call this m underscore hit react and when the player gets hurt we want to play that montage the way that you do that is go to functions on execute and all you want to do is cast the target to a third person character and the target the person that got hit needs to play the animation so what we'll do is we'll just play anon montage and the montage to play is the hit react so click on that and then check that box there and now inside of ge damage to get it to play that visual cue that we added you need to just go down to gameplay cues add a gameplay queue and then you want to add our took damage i'm going to run up to gameplay character here i'm going to punch him and you can see that he's playing the animation he's taking the damage everything seems to work pretty well except for my punch ability which always seems to mess but there you go so that's how a gameplay cue works and you can do a lot of stuff like spawn particles or you know i mean literally anything pretty much whatever you want the effect to be you can do it so here's a really powerful thing because attack power as an attribute why don't we hook that up to our damage instead of just having a value of 20 right and that way we can add buffs and debuffs that make you do more or less damage right now we're adding some power some flexibility in there you can see by default we're just taking 20 health away from the player let's click the drop down and click on attribute based what that lets us do is instead of using a fixed value we can actually select the attack power attribute and we're going to type negative 1 as a coefficient so that it takes the health away instead of adding it we don't want to give people health when we punch them right so negative one and so now whatever our attack power is is how much damage we'll do and so now we can add even more gameplay effects like let's do this let's go to gameplay effect and i'll add one i'll call it ge attack debuff and what i'll do is i'll modify your attack power i'll divide it by two so i'll halve your attack power so now we've just created our first debuff right let me show you so under character defaults we do 20 attack damage right so when i punch someone it'll deal 20 damage but if i give myself the attack debuff then i'll only deal 10 damage the opposite could be true you could buff the player's attack power and so this sort of stuff is like really powerful and you can really build some cool gameplay stuff out of this because this is a debuff as well let's make it have a duration instead of being an instant effect go down to has duration and then we'll just make this effect last for like 20 seconds right so now it's going to debuff your character for 20 seconds and then it will remove this right so it's going to divide your attack power down to 10 but then after 20 seconds it'll go back to where it was you can actually see this working in the game here so i've spawned in the debuff has applied you can see my attack power is 10 and it just blipped back up to 20 because the duration was over so it'll reverse itself now okay so here we are attacking the other player you can see their health went down to 90 because they have the debuff you can actually see debuff shows up as well and once the debuff wears off just give it a second there we go so our attack power is now 20 and now it should take us down to 70 health when i hit him and there you go so there's the debuff working and so that's pretty much it we've really just scratched the surface so you can go so much deeper than this and i encourage you to i will link uh in the description guest documentation it's this crazy project that i showed you at the start really good tons of source code tons of writing up and you could i don't know you can read it for weeks and it's crazy like the amount of detail is also so i'll link that in the description if you have any questions feel free to ask and i'll see you guys later if you would like to help fund my private elevator please feel free to subscribe or buy my course be really helpful it's not a very good elevator oh my legs are getting sore now don't know what
Info
Channel: reubs
Views: 47,950
Rating: undefined out of 5
Keywords: Unreal, Engine, C++, Tutorial, Beginner, Game, Programming
Id: Yub52f4ZUU0
Channel Id: undefined
Length: 38min 57sec (2337 seconds)
Published: Thu Sep 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.