Submind YouTube summaries
Thumbnail for (Ep 7.1) Buy Max Implementation! - Unity C# Idle Game Tutorial Series [2021 Edition]

(Ep 7.1) Buy Max Implementation! - Unity C# Idle Game Tutorial Series [2021 Edition]

Watch on YouTube

Video summary

In this episode of the Unity C# Idle Game Tutorial Series, the creator focuses on implementing the mathematical equations for purchasing maximum upgrades derived in the previous video. The primary goal is to translate these formulas into functional code within two specific scripts: one handling the core logic and another managing the upgrade system. The developer begins by defining a method that calculates the cost of an upgrade based on a base cost, a multiplier, and the current level, utilizing `BigDouble` types to ensure precision with large numbers. A second equation is introduced to solve for the maximum number of upgrades purchasable given a specific currency amount, which is essential for determining how many times an upgrade can be bought instantly. The code implementation involves simplifying these mathematical expressions by creating helper methods and carefully managing variable references using `ref` keywords to ensure that changes made within the calculation logic correctly update the player's actual currency and level variables outside the method scope. The tutorial then addresses a critical issue encountered when attempting to apply this logic to a list of upgrades in the Upgrades Manager script. Initially, the developer tries to iterate through an array of upgrades and call the buy method, but encounters an error because the loop index represents a temporary value that cannot be modified directly with `ref`. To resolve this, the code is refactored to explicitly access the list item by its index within the loop, ensuring that the correct upgrade object is targeted for each iteration. This process also requires adapting the data types used in the calculations; specifically, since production upgrades can result in non-integer levels, the level variable must be treated as a `BigDouble` rather than an integer, while the cost calculation remains compatible with integer inputs where appropriate. After correcting the logic to handle these type differences and ensuring the upgrade lists are properly accessed, the mathematical formulas are successfully integrated into the loop structure. A significant portion of the video is dedicated to debugging a mathematical error that caused the entire buying system to fail. The creator admits to placing a "+1" term incorrectly within the fraction during the derivation of the maximum purchase equation, which resulted in broken calculations when tested in the game. Upon realizing the mistake, the developer moves the "+1" outside the numerator and inside the logarithm component of the formula, correcting the logic for all three types of upgrades: click, production, and generator. Once this algebraic error is fixed and the user interface updates are synchronized with the new calculations, the system functions perfectly. The final result allows players to instantly purchase as many upgrades as their currency permits, providing a powerful quality-of-life feature that streamlines gameplay by removing the need for repetitive manual purchases.
Read the full video transcript
hey everyone it is crypto grounds here and welcome back to another video this is episode 7.1 for the idol game tutorial series 2021 edition and today we're going to be using the two by max equations we came up with in the last video 7.1 and implementing them in code okay so let's jump into it so we are currently in our project right now i have created a brand new button that's right here it's just going to be our buy max button and we're going to create two methods one will be inside of our method script so let's open that real quick and then the other one will be in our upgrades manager script so let's start here so let's create a method for our bimax okay so currently it has no parameters whatsoever however let's take a look at our little equation here so i used ascii math to format this equation looks very nice so let's start with that let me split this up let's implement our uh parameters so we don't need a cost we just need our base multiplier and our current level so our base is going to be well we can either make it a double or a big double but let's just be consistent and use big doubles if you wanted to use less memory our base molts and levels could be less than that like levels could be ins our multipliers could be floats and our bases can be doubles that's however you want to do it i'm just going to keep everything consistent and use big doubles but that's for you to decide so we have a base cost a cost mult and an int level so let's create our equation in code so we're going to have our big double cost and this is going to be equal to base cost times and just write it all out okay so we have our equation so the cost is equal to the base cost times big double dot pow cost molt and level multiplied by our big double dot pow cost mult n minus one and these two terms divided by our cos mole minus one so we now need to implement our n so this is where the other equation comes into play and that's this guy so let me write this out also for this equation we have c so our c is going to be the currency we are willing to spend so you want to add that as a parameter as well so we'll just call that currency all right so here's our equation fully written out and if you were to compare to the ascii version of it you'll see that they are identical so yeah it looks like that and there's some simplification we can do so i've already done one and that is make this cost method right here which is just the base times m to the power of level we can do that here as well so we can just replace this equation here or this part of the equation right here base cost times big double dot pow cost mole level and replace it with this method and my n is an int and i also ended up changing the cost volts to a float just because i don't really think most people are going to have super large cost multipliers and if that's the case use doubles whatever suits your needs and yeah so these two are the equations that we will be using and if you want you can press alt enter to import static members and it kind of cleans things up a little bit and what that does is that it implements the using static break double at the top of your script one last thing you can do is that if this is really lengthy for you as well you can change the names of our parameters so currency could be c instead our base cost could be b or m it could be cos molt and our level could be l this may be a little hard to read but at least it's not super long like it was before and you could just make a comment so we have our equations done let's do the actual buying process so we still want to check to see if our cost or if our currency is greater than this cost because if for some reason your currency changes while this is running and you can't actually purchase this we will get negative numbers and we don't want that so we're going to do is check to see if c is less than cost and if that's the case we're going to return and do nothing otherwise we're going to subtract cost from c and we're going to add to our level and this will be based on whatever n is so there's two things that we need to do before this is done is that these values aren't actually going to change the actual currency you have and the level what we need to do is move these to the beginning so we have cl and then our bm and then we add the ref term so basically we're grabbing the reference of our currency and our level variable and changing them directly inside of this method we didn't have these ref terms and if we were trying to change c and l inside of this method it's just not going to do anything on the other side and this is our buy max method that is all we need to do second thing we need to do is head to our upgrades manager we need to implement like actually buy max so i'm going to merge the buy max like all together and it'll buy max all the click upgrades production upgrades and generator upgrades you could split it up if you'd like but i'm just going to do it all in one just to make things easier so i don't create more buttons all right so i have created a bimax method with no parameters and what we're going to basically do is for loop through all of the upgrades and call that by max method that we made in our method script all right so i have implemented the three for loops for our bimxes and we ran into an issue our error here is that index or access returns temporary value so this is actually a temporary value and using the ref term doesn't change it directly and we need to do something else so let's head back to our method script we can keep this by max method because if we were to have just a normal level that was either an array or just an integer this would work just fine however we need to grab the list and do something else so we can just copy and paste this and change this int into a list and on top of that we need to grab the index as well and we can just label that as i so now we go to the bottom right here and we are accessing the list at index i and we're going to be doing the same thing for our cost equation now if we were to head back to our upgrades manager oh we have one more silly me if we were to go back things should be fine oh except it's not we have to get rid of this after that we need to add our index which is just i oh okay i see here our production upgrade is actually a big double because our generator produces the up the production upgrades i think it's it's been a long time since i've touched this so and that is to copy and paste this one more time except with an int list we're making a big double list so and since our levels are not whole numbers we don't have to cast this as an int anymore so our n becomes a big double and our cost right here wants an int so we need to copy and paste this cost equation change our level to big double and then we should be safe and now if we go back to our upgrades manager everything should be happy and let's give this a test so before we test it we have to assign this buy max method to this button drag your upgrades manager to the on click and select the buy max method and hit play alrighty so let's just get some stuff and if we were to hit by max oh boy okay so this is broken this is very broken i see interesting okay that's not good all right so i kind of realized i did something really silly um in regards to this buying step right here or this you know figuring out this equation right here but in step three where i add one to both sides i added the plus one in the wrong spot so you see that plus one right there well that should actually be on the outside of this you know this fraction so it should actually be right here so yeah i i did some testing and it completely broke and it's because this is incorrect so don't make the silly mistake um make sure you check your math and if you do mess up one step you can do something wrong like i just did so let's move this plus one which i got to do for all of them so give me one second all right so it seems to be working fine now let's see yeah it should be working fine it's just not updating the ui so this is what the final equation looks like so i basically just move that plus one outside of the numerator inside of this log so it's all of this plus one and make sure you change it for all of them and if we were to go to our upgrades manager we need to update our ui so in which we just add that at the bottom and everything should be working just fine now sorry for making that math mistake i actually feel really embarrassed about that because it's just a plus it's it's literally just basic addition and i couldn't even do that right but anyways this should work now so we can just spam this a ton let's just give myself tons of money tons of flash bimax and yeah everything's great cool so everything's in good condition let's just give us some of this cool all right so that is the bi-max it is very powerful and it's a great piece of quality of life to include in your game and i hope this wasn't too complicated so i'm going to leave these up for a bit just to make sure you have all of them and you may not be using this one right now but later down the road i guarantee you're going to be using something like this where you are taking in just a single integer level rather than an entire list anyways that is it for this video i hope it was really helpful and if you enjoyed it and you found it helpful please leave a like and comment your favorite fruit in the comments below as it really helps out the video subscribe to the channel if you haven't already and turn on that bell if you want to be notified for future tutorials and other live streams and if you want to support the channel click that thanks button below or check out the patreon in the description below y'all are the best and i will see you guys in episode 7.2 and according to my handy-dandy little plan i have created for the upload schedule we'll be working on the buy max um we'll be working on a buy mac system where you can either buy 5 10 100 or maximum so you'll be able to change how much you can buy for each upgrade so this will be pretty nice as well so yeah that'll be the next video anyways i can't wait to make this video and i'll see you all there peace see i'll be up in class but my mind is in the clouds though know the teacher's mad cause my music being loud tell me keep it down say i kill it on the down low and if i turn it up then i'm bound to attract the crowd so no wonder me and then be out of state doing things you can't imagine chris angel on the mic give me a beat i'll show you magic