C# Godot 2D Platformer | Magic System Pt 1 : Laying The Groundwork

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys this is mitch with fine point cgi  and today we're gonna talk about how to do   a basic magic system inside of godot now it's  not gonna be a super crazy one what we're gonna   do is we're going to set up our magic system to  go off of our dash so every time we dash it's   going to reduce our magic points right our  magic values by a set value so in our case   it's going to be 10 and then we're also going to  set up the ability to have our mana regenerate   and we are going to do a little bit  of refactoring of some of our code   in preparation of kind of making this to more of  a production level game so that's what i have in   store for you guys today so let's go ahead and get  started okay so the first thing we're going to do   is we're going to do a little bit of housekeeping  on our player controller so we're going to do   some changes to how the dash system works  a little bit to help make it a little bit   better and and easier for us to maintain and a  little bit more efficient than how it was before   so what we're going to do is we are going to go  out to our player controller so let's go ahead and   open that up let me resize this for you guys all  right and we're going to come down to our movement   code here process movement we're going to add in a  bit of code here so the first thing that i'm going   to add is i'm going to take my facing direction  i'm going to change that into a vector 2.   so let's go up to where that  is change this to a vector 2.   all right and what we're going to do is we're  going to say hey that's going to be equal to a new vector 2 and we're going to say 0 0 that  way just by default it's created and now we need   to come in here and make this into a new vector  2 and put in our two commas here there are two   zeros here and then we need to go ahead and go out  to our facing direction and change that to dot x   and then let's take this and change it to a dot x  as well so we're going to scroll down and we are   going to take a look at some of our dashing code  here now you can see that we're kind of hey if ui   left ui right ui up ui right and up ui left and  up and then we're doing our stuff here and that's   really inefficient and really poor code  design on my part i really shouldn't have this   like this right it just doesn't make a lot of  sense so instead let's go ahead and map our   inputs much like we do up here right let's match  our inputs up and bring some of this process dash   code actually all of it really into our process  movement code that way we can kind of condense our   code and make things a little bit more efficient  and a little nicer so we already have ui left   in ui right right so what if we just wanted to  add ui up and ui down right because we have ui   well i guess technically we don't need to really  process ui down because i don't know if you'd ever   dash down so i guess we could just use do ui  up so if we actually just take ui up and just   copy that and bring that up here and paste it and  now we're saying hey if you hit left right or up   then we're going to change our facing direction  but we can't get away with saying facing direction   is equal to -1 and facing direction equals to  positive one because now we're taking this and   moving it into more of a three-dimensional or i  guess in this case a two-dimensional object right   so instead of us saying hey the person  is going up left or right they're going left right or up or they're going left  or up or they're going up or right   right so we need to instead of doing a  integer right we need to do a vector 2   because we need to be able to handle y axis  changes and x axis changes if that makes sense   so and it'll make a lot more sense as we go  but the first thing we need to do is we need   to change our facing direction to a vector 2. so  if we scroll all the way up to the top and we say   private vector 2 facing direction is equal to new  vector 2 and we say 0 0 and we'll just go ahead   and copy this because we're going to need to do  that down here as well so we'll scroll down to the   bottom here down to our movement code and paste in  that new vector 2. and since ui left is on our x   axis let's go ahead and just go dot x and then  because ui right is on our x-axis we'll say dot   x as well and now we can come in here and  delete this little bit here and we can say   basing direction dot y is equal to one because  we're going up right and then we can also   we don't have to worry about our animated  sprite because we're not going to flip our   sprite if we're going up right there's  no sprite for up so we're good there   now we need to say hey if our facing direction  does not equal zero right we can't do that   anymore we need to say dot x right because we  want to say hey if our facing direction dot x   is equal to zero so if our character is moving  on our x-axis we then need to play our animation   and we need to face a specific direction and  move our character so now we could say velocity.x   math lerp right uh velocity.x facing direction  dot x times speed awesome so now that we have that   now we have a basic way to move left and right  by using a vector 2 here instead of using our   integer so we got all that at least working  now we need to go ahead and bring in our   dash code right like we did before so what we can  do is if we look at our dash code you can see that   we're adding in our dash speed so if we come up  here instead of doing how we were we're saying hey check for all of these cool variables and if they  hit shift go ahead and apply them right that's how   we were doing it before well instead what we could  do is we can go ahead and say if input oops input   dot is action just pressed quote dash and then  we can come up here and say if is dash available   then i want to say is dashing is equal to  true because we are actually starting our   dash right now we're allowing our character  to dash and then we're going to say dash   timer is equal to dash time whoops dash  timer reset come on dash timer reset   is dash available is equal to false and then  we should be good for now awesome so now that   we have that we can go ahead and come down here  and say or facing direction dot y does not equal zero then we can do some fun stuff here  so we can say here is hey if i'm dashing   right then velocity dot x  is equal to my dash speed times my facing direction dot x and then  my velocity dot y is equal to my dash   speed times my facing direction dot y and what  that's going to do is that's going to allow me to   control how my velocity is going to change based  off of my dashing and then i'm going to say else   do our normal movement like that and what that's going to do is it's going  to simplify our dash code a lot so now   if we go to our process dash here we find our  reference which is right here is dash available   and we comment this out and now it's commented out  of our code we can go ahead and run this and our   dashing should work and be slightly more efficient  although we do have a compiler error oh what does   it say oh facing direction dot x i missed an  x for when i take damage okay let's try that   again so let's go ahead and play cool so now if  i dash you'll see that i can dash left and right and it seems to be working at least somewhat  right but you'll notice that a dashing   up dashes down which isn't correct and  dashing down doesn't dash up so that's strange   so why is that well let's go ahead and take a look  at the code and let's find out so if we scroll up   let's go ahead and take a look at some of our  code here now we're no longer using process dash   so it's always good to delete code that's not  being used anymore so this code is useless to   us so let's go ahead and get rid of it cleaning  up our code basically a little bit if we uh   keep scrolling up you'll see here there is  dashing and then we're running through our   normal stuff and then we're saying hey if  dash time right velocity is zero so we stop   our velocity and then we're saying hey else if  is climbing add some gravity gravity is usually   what's pulling us down so let's see what happens  if we do else is climbing and is not dashing and we had that issue where our movement  was flipped so let's go ahead and do   minus one instead for y up or y  down i guess technically speaking   so let's go ahead and run that  and let's see how that goes so if i hit up and dash you'll see that i can dash  up now if i land i can dash to the left and i can   dash to the right and i can dash in the corner and  dash in the corner perfect that's exactly what we   wanted so now we can dash in three well i guess  three of the four directions but i'd like to dash   down i don't know if i'd ever really need to dash  down but let's go ahead and add that in it's only   gonna take us two seconds to add it in so let's  just go ahead and copy this paste it ui down and then face direction one and we don't need to  test that right i mean we it'll work hopefully   so we'll keep it like that so now that uh we put  all that together we've really narrowed down our   code and made it more efficient and you'll see  as time goes on i'm going to start refactoring   this code and we're going to start making it  a more sophisticated and more optimized system   as time goes on so you know be sure to pay  attention to all the tutorial videos because   i'm going to be doing this for a lot of the stuff  as time goes on we'll we'll keep expanding on this   project and make it into something truly special  so now that we have this let's go ahead and add   in some magic so what we need to do is we need  to make it so the person can only dash so much   right and i'd like to eventually add in magic  attacks and things like that in the future   so we need to add a magic system for the player  to have right so the player needs to have like   a mana amount and like if they dash it's going  to reduce their mana amount right so let's go   ahead and add some of that let's add in some mana  regeneration if we want it and i think that'll be   what we'll do for this next little section  so let's scroll up to the top and add in our mana here so we'll just say private float  mana is equal to and we'll say a hundred i think   that'll be a good number and then we also need to  describe what our maximum mana is so private float max mana is equal to and we're gonna put it at a  hundred f for right now oops missed my semi-colon   whoops missed my semicolon again all right so now  that we have these two things let's scroll down   and let's go ahead and set up our using like  usage of our mana so if we scroll down to   our dashing section here we can go ahead and start  removing some mana when the user uses it right   so what we're going to do is we're going to say  hey if dash is available and mana is greater   than whatever value we want to put as our minimum  amount of mana to dash now in this case i'm going   to say it's going to take 10 mana to dash now  i'm probably going to pull this out here into   some kind of variable or object or something  later but right now we'll focus on just getting   it to work so we'll say hey if man is above 10  then we're good to go and now what we need to do   is we need to say mana is mine or minus equals  the 10 that dashing costs and now if we print now if we go ahead and we print  our mana when we do our dashing   you'll see that when we dash you'll see that  now we have 90 right here so if we dash again   we have 80 we have 70 oh i got shot i wasn't  paying attention i was reading the stuff 60   50 40 30 20 10 and then if we dash you'll notice  that we can't dash because we're too low on mana   which is a bug if you've noticed that we have 10  mana we should be able to dash right where we're   above the or we are at the required amount so  we know that this works but we also know that we   need to adjust some of our code so let's go ahead  and go back into visual studio here and let's say   mana is greater than or equal to 10 it can go  ahead and do the dash and now that we know that   our mana can drop down below 10 let's go ahead and  set up a mana regeneration system so if the user's   been sitting for a little bit of time or something  or kind of like with like if you think a halo or   something like that when your shields get low  it kind of stays there for a second it kind of   refills that's kind of what i'd like to do so  let's go ahead and build that real quick as well   so if we scroll up to our velocity section  here so hey if we're not climbing or dashing   right else you know if we're not doing any of this  stuff right we can come down here and we can say   if mana is less than 100 i want it to attempt  to fill up my mana so i'll say mana plus equals   delta times 1 and what that's going to do  is that's going to give me i know that it's   times 1 and you're going why are you times  you get by one it's just going to be delta   that's because in the future i'm going to make  this a variable so that way we can actually   um increase the amount of mana the player can can  uh regenerate in a period of time if we want to   so a lot of these values here  are going to be switched out   but we're just building in the base code  here so then i'm going to say gd dot print   mana so now that we've saved this let's go ahead  and take a look at it so right now our mana is 100   so we're not regenerating any mana if we teleport  all right now we're regenerating mana see that sweet so that means that we could dash land dash   dash dash dash dash dash dash you can  see how it'll slowly but surely refill   for the user which is really cool but  what if we want to do a pause you see how   it's immediate refilling it's not waiting for  the player it's just refilling immediately   so how would we go about refilling it and having  it wait well what we can do is we can create   what's called a stamina timer right a timer that's  a set timer to wait until it starts doing its   refilling so the first thing that we're going  to do is we need to create some of our global   variables here for this timer so we're going  to scroll up to the top and we're going to say private float mana timer reset because we need to  have a timer reset much like we did in a previous   video we did like our climb timer and climb timer  reset it's basically the same thing just slightly   different so we'll say mana timer reset let's  say two seconds i think two seconds is a good   time and then we're gonna say private float mana  timer is equal to 2f so the initial uh dash will   be a two second delay before it starts uh building  up some mana here and then we need to come down to   our movement code we can come down to here and we  can say man a timer is equal to mana timer reset   and that's going to allow us to reset our timer  so that way when the user dashes we don't suddenly   allow them to re to uh recharge immediately  we are resetting it every time they dash   so you can dash but because you dashed and used  your magic it requires you to reset your mana   so to me that makes it a little bit more risk  reward you can either try to get out of the fight   or you can and not get your mana back or you  could try to stay into the fight let's say and   and potentially regain some mana so now we need  to scroll up to our mana section here so if mana   is less than a hundred we need to  set up if it's less than a hundred   we're going to fill it up but we need to also  make sure that our mana timer has elapsed   right so that way the mana timer has actually  finished its time frame so we'll say and and mana timer is less than or equal to zero and then  we need to say hey if it's not less than or equal   to zero we need to start running that timer right  we need to start running that timer to get it to   zero as quickly as possible right because we want  them to get their mana back so we need to say else   if mana does not equal a hundred then i need you  to go ahead and make your mana timer be reduced by delta time and what that's going to do is it's  going to say hey let's reduce the mana timer by   delta which means it's reducing by seconds now we  could actually add in if we did times let's say 1   or whatever value we wanted we could put in a 2  if we wanted it to be reduced in half right if   we want to come back faster so say they picked  up a power up or something right we could make   that a variable and allow them to override  that value if that makes sense now obviously   i'm going to keep it like this for now and we'll  get into that when we get into power ups and and   features and things like that later in the uh  tutorial series right but from here we should have   everything we need for our mana timer to work so  if we open up our godot and then go ahead and dash you'll see it says 90 and now two seconds have  elapsed and i'm starting to gain mana again   perfect that's exactly what we wanted simple  enough now we do have some code that is being   reused in here so we really should refactor  some of this code so if we go take a look at   it we have mana plus equals delta one and then  we also have mana minus equals ten and we're not   doing any error checking right so the the player  in theory could reduce it below ten or above 100   in theory and we don't want that right we want the  ability to control that so let's create a function   to update our mana and just call that instead  because it makes things a little easier when   it's out in a function so we'll come over  here and say public void and i'm going to   make it public and the reason why i'm making it  public is so that the game manager or anything   really can touch that specific function and  update the player's stamina at any time it allows   us a lot of flexibility there so that way we can  mess around with that kind of stuff so we'll say   update mana and then we're going  to pass in a float and we will say mana amount and then we're going to say mana plus equals mana amount and then we're going  to say hey if mana is greater than or equal to max mana then i want mana equal to max mana else if mana is less than or equal to zero then  i want mana equal to zero and what that's going   to do is that's going to force it between the  two values that we have you can either have   your total mana amount or you can have zero that  way you can't be outside of those bounds it kind   of forces us into those two little bounds there  and i'm going to go ahead and copy update mana   i'm going to scroll up to where i do my dash  and instead of doing mana minus equals 10 i'm   going to say update mana 10 and then i'm going  to scroll up and i am going to find where my mana   is getting added right here so i'll paste that in  and then we'll say plus equals our delta plus one   there we go and it should work exactly the  same so now if we go back into our game here   and we run our project we  dash you see it's waiting oh we're getting a hundred mana uh oh i made  a mistake somewhere let's take a look my guess   is i flipped it didn't i oh that's what i forgot  update mana minus 10 not 10 because that's going   to give you plus 10. so now let's go ahead  and try doing that again and it should work all right now we're at 90. sweet very cool so you'll notice that there is  one bug that we need to look at is if i hit   up you'll see that the character is running which  is a problem because the character is not actually   running right he's just sitting in place so we  need to fix that so let's go ahead and close that   and take a look at our player controller  here if we come down here and we go ahead   and check out our hey if not in air let's  say if not in air and facing direction.x   does not equal zero and that will force  the player to not play its animation unless   it's moving either left or right so now if we do  that that'll fix that little issue so if we hit up   you'll see that it doesn't do anything if we hit  left and right the character runs sweet all right   so what did we do today well we created a small  mana system we did some refactoring of some code   and we made it so that our mana would  regenerate after a delayed amount of time   so in the next video in this series we're going  to go through making a potion so that way the user   can walk over to it and hit the x key or the enter  key or whichever key and allow them to pick that   up and restore their mana by 20 points so that way  we can have kind of a pickupable object so that's   what's going to be going on in the next video  hopefully i'll have that one released like on   friday or maybe that following monday so it should  be pretty quick but that's all i have for you guys   today so if you like this video go ahead and  hit that like button hey you know if you dislike   this video go ahead hit that dislike button  because i am here to make content for you guys   and as always this was a viewer suggested video so  if you guys have any suggestions or anything you'd   like me to cover let me know in the comments  below and i'd be more than happy to get to it   this little c sharp platformer series is going  to be ongoing so if there's any cool features you   think would be awesome to see let me know and i'll  be more than happy to run through and build them and hey if you have any questions about any  of this let me know in the comments below or   hit me up on my discord link is in the description  we got some really cool guys out there and   you know they always love talking to people  but that's all i have for you guys today   so thank you so much again for watching  and i will see you all next time thanks you
Info
Channel: FinePointCGI
Views: 430
Rating: undefined out of 5
Keywords: c tutorial, c programming language, c programming language tutorial, c programming language tutorial for beginners, c programming course, c (programming language), programming language (software genre), tutorial, beginners, programming, examples, language basics, literals, data types, functions, loops, arrays, pointers, structures, for loop, while loop, do while loop, user input, if, switch, p3 x viden, dr, danmarks radio, p3, drp3, p3 videnskab
Id: 1JZOnCZuAv8
Channel Id: undefined
Length: 28min 36sec (1716 seconds)
Published: Mon Oct 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.