IF-ELSE like never before | Beginner to Pro

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone today we are going to talk about if else and it's going to be a pretty fun video I hope so and you let me know if you like this kind of video because we can cover like this every single subject it's going to be a lot of fun so let's get started if else and at the end there is a plot twist okay so be ready for the plot twist a c condition let's take an example bull done equals is done if done we do something and then after that we always do something else this is one way to write things of course there's a clear Improvement here uh which is that the C braces always go in the same line as the if itself this is pretty obvious anyway let's continue if you want an lse you know like if something else we do something else this is pretty obvious you already know that now the bull done is done is pretty simple so maybe you could do just put the is done into the condition itself so you just say if is done and then you do something I do something else now this can become sometimes a little bit out of control you know like you have oh is done and is grounded and you have enough stamina and you have no debuff that is saying that you are dizzy then you do something otherwise we do this and then we always do that last thing this could be improved you could split it into multiple booleans before that so you could have like one is ready that is like just is done is ground and the stamina and then one is dizzy that contains the check on the debuff and now your if is much simpler you have like if is ready and and is not dizzy this is pretty much easier to understand sometimes than this which is pretty long um so this is nice you know this is a nice way to make things readable let's continue now conditions is a condition is branching you know like every time you put an if you can obviously go One Direction or another Direction and this adds a little bit of complexity in your game and depending of how much you Branch how much nesting you you do uh and this can become a little bit sometimes complicated so here we have a simple branching right we have one condition with an L statement and inside that if we have another if with another lse so that's already pretty much a lot of branching but still manageable you know but I mean everybody saw sometimes codes of branching or nested statements that go you know like five six seven levels deep that's not really fun to to work with um so there are ways to improve that one way is to use conditions as guards the guard is kind of like an early aboard mission statement you know so instead of here I go back input is not equal n null and input do length is greater than zero we reverse the check and we write well if it is null or the input is zero then we will return We aboard the mission we get out of the function as fast as possible and if the guard well doesn't do anything then you just continue and you still have kind of the same uh branching in terms of logic but in terms of readability you are gaining quite a lot and uh I like this guards are pretty nice especially if you have a lot of guards sometimes in functions this is a good way to organize things so early returns are great simple now if I take the example from before you know we could also reverse it so instead of saying bull is ready bull is dizzy and then if it's ready and not dizzy we do something otherwise we do something else you know maybe you could refactor it in oh if you're are not ready or you're dizzy well then we return we don't do anything here we return and otherwise we will continue so this how do we write it as a God and it it's easier in some cases it's easier it's really like very clean uh I feel to read and um to understand I like this a lot now let's see another example sometimes your code starts like this I have a method compute power that takes an ability and returns an ability package so an ability package will just return uh the ability name and it will return some computed value or multiply if you want of how much attack uh this thing should do just an example uh you will see why soon so if the ability is called a fireball and the player has enough Mana then we return a new ability package I know there are some obvious ways to refractor this but this is not the goal of this video this this for later so sometimes you start like this and then very s very easily uh very quickly it can become uh this like oh I have a second spell I have a second ability a leap well then if it's a leap I will check also if there's enough stamina and I will return another ability package so now we have the if return right it's like a guard and if this is not matching well you have the next if return well H if you have two abilities you can also put three abilities right maybe I have also a fierce Fireball I still check the Mana but this one has a magic multiply at the end of 1.2 you know and while three abilities why not four abilities you know maybe I have also a snowball that does just very little damage is this going out of control like obviously from a software engineering let's say principle point of view there's an there are obvious ways to refractor this but here's the funny thing I can go on and on and on ADD tens of spells hundreds of spells here in this video just under those and the funny thing will be that you will be able to keep up with uh everything that is happening and you will be able to understand everything and it's very easy to just change things to extend it to maintain it so it's really readable and easy to understand it's not the best pattern you know it's like kind of an anti- pattern doing a string check you know who who who the hell does string checks like that this should be a class obviously you know but uh this is pretty simple to understand and to extend and it's not that bad of a code you know so and why do I show you this well I show you this because a few days ago this post went viral from a game called batal batal it's a poker game where you can cheat basically so you have cards and every card has some multiplier or whatever and the this tweet went viral where they showed the cards. Lua logic of that game and it's a big file with 4,700 lines of code in Lua and it has a huge sequence a huge stack of if return statement if return if return if disabili like that and some condition will return if return if return so it's it's a great example of it might not be the best software pattern but this is highly readable and it is shipping batro is a very successful game and they're shipping so what is the the conclusion of this video if you want to ship games just use a lot of e return you know don't worry too much about how your code looks like it's more valuable to avoid spaghetti cat which is like too many things depending on each other then just you know like sometimes a bit veros way of writing things so if return this is how you ship games all right I hope you enjoyed this video thank you very much for watching please leave a like And subscribe to get more content like this uh if you like this style of video particularly uh well then leave a comment below there's many more things in C to cover in a fun way in a serious way in a sarcastic way keep it up and go back and work on your game I'll see you next time [Music]
Info
Channel: This is GameDev
Views: 1,038
Rating: undefined out of 5
Keywords: make my dream game, how to make a game, made with unity, game devlog, gamedev vlog gamedev, indiegamedev, game dev, indie gamedev, indie game, unity 3d, unity3d, game development, make a game, tutorial, guide, game engine, unity hub, blender 2.8, blender 3.0, blender 3, madewithunity, iOS, Android, mobile game, smartphone game brackeys, thomas brush, jason weimann, jonas tyroller, dani, danidev, blackthornprod gamedev, unity, install, how to, install unity
Id: FFC_meE5u14
Channel Id: undefined
Length: 7min 57sec (477 seconds)
Published: Fri Apr 26 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.