 Hello guys welcome back to another idle game tutorial. This is part of the idle game tutorial series This is episode 10 today. We're going to be doing some code management and optimization This is some good cleanup because I think the code needs a little bit of work since I've been focusing on the basics Previously we we did a good optimization and UI practice UI practices and Yeah, let's get right into it. So we're just gonna hop straight into our code today. We're completely dedicating That's the code So anyways Basically we have a lot of stuff that can be cleaned up and shortened and stuff like that and We're not gonna be really changing anything fundamentally who are sick of using pull and void update and the major methods So first of all, we're gonna start with a v-sync. It's very important because if you have your your update method running Every frame or pass that it would use a lot of CPU. So we're gonna simply Just do application dot set frame, right? Oh No, it's application dot target frame rate equals 60 So this Uses v-sync to make sure our frame rate is stable at around 60 60 to 100 it's probably its approximate range but v-sync It decreases the CPU usage by a ton on my game It was using about 50% until I applied this it was then using like around 3% So that's very very helpful And make sure it's in the start method so that it'll be used every time you start your game And you can change this to 15 30 60 120 and maybe 144 anything between that will be rounded up to the next so 45 will be rounded up to 60 and For if you want to gain still be smooth then just do 60 a lot of people say just use 30 since it won't matter But I personally use 60 and if you have settings in your game then use it I Be nice to choose differently some people would prefer less Anyways, we're done with that. Let's get some code optimization Let's just get rid of some parentheses since it's unneeded So now we want to look for repetition right here. We've seen this multiple times So we're gonna copy and paste this and we're gonna make a method out of it We're gonna do public We're updating a string and Let's see we're gonna call it Update number. Oh, we're gonna call it our Or we're gonna call it our notation. I say I guess you can call it a view on So now We're gonna be importing two things The first one is going to be our variable double and Then we're also gonna have we're gonna call this X We're also gonna import a Float no, it's actually a string. I forgot And it's gonna be a lot The string is gonna be our format because we'll be able to know how many decimals you want on default So we're gonna take this out actually we didn't delay anything, right? No, we're good So basically we're going to Replace this with return We don't need this right here and For our else we're going to return This right here We don't need any of this extra stuff since we'll have that love We don't need this else Because if this is not successful or if it's not true then it will run this return statement So now our Y is simply going to go here our X is going to replace the coins value Yeah, what is this? I don't want this and we're gonna remove some of these Useless parentheses Qualifiers redundant. Okay. I guess we don't need our system anymore for some strange reason even though it's asking for it A few episodes ago All right, we have our notation method and it's very short and simple We don't need all this nonsense up here So since we're returning our string right here basically We're gonna take this Dicom no, we're gonna start here. We're gonna start with this click value text right here Okay, so basically we're gonna replace this right here with the method that we just made and since it and it takes a double It'll be first and then the next will be how many decibels which is going to be f0 and That's it. That's all it is. We just changed this whole five line six line code to just one and We can do that for others too So before we continue We are gonna do something called overriding. So we're gonna have some Methods that are actually gonna need to take it in a float instead of a double So just in case we're gonna copy and paste it and we're gonna place this double with the float And that's it. Oh, and also these have to be math F math F That's it So now if we so now if we do the same exact method But it's a float then it'll override this right here and I'll call this one right here Yeah, that's it and That really depends if we need it or not You can literally do the same thing with ins as well but I Never use ins so Let's get replacing So next is gonna be coins. We have that You also want to change the beginning text to before you replace everything. Okay here We don't have anything up to here and this is gonna make the code a lot a lot cleaner All right, so we're in a little tricky situation right here. Actually, no, we're not never mind scratch what I was saying Let's keep going This isn't zero right here. All right, we can delete that easy-peasy See do you have any more? I don't think so We could delete some of this anyways All right, so we're good with our text updates All right, so we're gonna go to the upgrades now This is gonna look a little weird because we just had a whole entire Upgrade system and we're about to change it awesome So I'm only gonna mess with the main by click upgrades and the production Actually, since we have a by max I'm gonna do the production first so this looks easy on us So we're gonna make a new method called public void by upgrade So I'll have to show you something cool when it comes to the editor in In here we can do with upgrade or with buttons and methods that have like a that's taking a string or something like that We're gonna do string upgrade ID so we know what upgrade we're buying and We're gonna make this one P1 or gonna make this one P2. All right, simple as that All right, we have our string ID or our upgrade ID Sorry, it's three in the morning So we're gonna make a switch case of statements Which basically instead of doing if they're string or if upgrade ID equals P1 then we're gonna do this It's just like a sweat and it's L if L statements But it's a lot cleaner and it's easier to visualize and it's easier to do so switch If our upgrade is P1 Then we're going to do this stuff right here and to end this we need a break You can end this right here by returning about a value if this was like a public double and you were trying to get a Cost from a certain upgrade then you would use return whatever number, but we're not returning anything so we're just gonna break and We're gonna add a case and the reason why I want to add a break is because if we don't then it'll just keep going We'll keep running We're gonna add a P2 which will be here and we're gonna break That's it. We can get rid of these two upgrades right here so This is just good for just keeping everything in one method and it just overall looks better and We can add on to it whenever we want so we can do C2 for a click upgrade one or two We're just gonna copy paste this and Of course if you wanted to make your own methods to make this look cleaner and less repetitive you can do that But I think you guys need I think you guys understand the concept. I hope Why do we not have a thing right here? All right Why does it keep doing that? It doesn't look good All right, so we have our C2 upgrade done So our max our max how we're gonna handle this I think we're basically running the same thing Except we are going to Let's see. How are we doing this? You know what we can actually merge this we can merge this. I think about it We're gonna do the C1 and C1 max. All right So our C1 max is basically just gonna be our buy upgrade max a break and then our C1 is going to be that Then add a clean stuff up cost is defined below But I isn't it but I broke it what? Okay, so I guess we're gonna do cost one cost two. That's kind of silly So make sure you change all your variables so that they're different from the one above because apparently this break doesn't I guess that doesn't stop it I'm just gonna replace some stuff real quick Okay, so now We got rid of a bunch of methods and we just combine L and one so we're gonna go back to our editor and Our upgrades should be removed. So we're gonna have to go back to each upgrade and Assign a new method Which is gonna be our buy upgrade and the string for this is gonna be C1 So what's the cool thing is that you can literally just put in whatever upgrade you want and If in case you want something if it doesn't Have an upgrade you can do default and you can just do debug dot log I'm not Assigned to a proper Upgrade break Basically if your upgrade ID does not match any of these it will be called a default So we're gonna make that C1 The buy max is gonna be C1 max. So to make this faster. I'm just gonna highlight all my upgrades Assign them the buy upgrade click buy max C1 max Make sure they match or also won't work C2 P1 P2 and We should be good to go. So let's save it and run So if we buy it should work. Yep. See here we go. It's just calling the buy upgrade C1 See one right here. They all work awesome One one one. Okay So that was it for code optimization and cleaning up if you guys have any questions leave it down below they come down below and Make you hope you guys enjoyed this video make sure you subscribe to the channel and you turn on notifications So you know when the next episode is Anyways, thanks for watching. Hope you enjoy your day. Peace You