Advanced Rounding - GameMaker Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
my god michael i know you're long-winded but can you really feel like 15 minutes of air time just by talking about rounding oh ye little faith hello all the crazy people out there my name is michael i like wizards and dragons and making games let's talk about rounding so gamemaker has a few different uh rounding related functions built in and they all behave in slightly different ways uh first of course you have round which is uh similar although not identical to the way that most of you probably learned about rounding in math class when you were little uh there's also floor and this is for rounding a number down no matter what the decimal part is it will always take the decimal part chop it off and it will give you the um basically the integer part of a number so if you have uh something like 4.1 it'll be rounded down to four if you have 4.5 it'll still be rounded down to four if you have 4.999 it'll still be rounded down to four and then on the other hand you have seal which if you never thought about it before you have floor which is like the thing beneath your feet and you have seal which is the ceiling which is the thing that's overhead and that will do essentially the opposite of floor seal will round a number up to the next whole number so if you have 4.1 that will be rounded up to 5 if you have um if you have 4.5 that will be rounded up to five even if the decimal part is very very small like if you have 4.01 that will still be rounded up to five and as for round itself uh this is something that comes up once in a while and once in a while it will leave somebody very very confused if they're not expecting it uh whereas in elementary school you probably learned that if the fractional part of a number is one-half or greater you will always round up otherwise you will always round it down uh that is not quite how round works uh round in computer science generally works on what you might have heard as banker's rounding or statistical rounding in most cases it will behave the same as the round that you learned about in elementary school but if the fractional part of a number is exactly 0.5 let's say if we have our n is going to be equal to 0.5 and if we try to round that it will be rounded to the nearest even number instead so if i were to show message round of n actually this i said 4.5 didn't i if i were to show message the result of this uh we will have instead of five we're gonna have four as we see here and let me just throw game end on the end of this uh whereas if we have something like 5.5 the fractional part of this is still exactly one-half and we are going to instead round this to six because six is the nearest even number five is not an even number as you can see it's a small difference uh from the uh the rounding that you probably learned about in school over the course of very large data sets it gives a slightly more accurate result than if you just rounded to the nearest whole number that's the round function that's not what i'm here for today so the fact that gamemaker has these functions is nice but ultimately all three of them will only round to single numbers and sometimes when you're making games or sometimes when you're writing code in general you may want to instead round to the nearest let's say multiple of 10 or the next the nearest even number or if you're dealing with hexadecimal you might want to round to the nearest 16 or something and none of these functions provide a nice built-in way to do that you can't ask the round function to round to the nearest 16 but fortunately it is fairly simple to uh to do this ourselves the code to write it is not difficult so what i'm instead going to do and from here on out for the rest of the video anytime i say the word round um in reference to one of the rounding functions just imagine that this can work with either round floor or seal uh depending on whether you want to round up or down versus always rounding down versus always rounding up so if i wanted to round uh let's say 5.5 to the nearest multiple of 10 uh we can first n divided by 10 uh 10 being the number that we want to round to the nearest multiple of put this inside the rounding function and multiply it again at the end by 10. and when we run the game this is going to give us 10 because 10 is the nearest multiple of 10 to 5.5 if we were to instead put in something like 13.25 this is also going to give us 10 as you're going to see if we instead give it something like 83.25 actually let's round up let's make it 88.25 this is going to give us 90. as you can see and this will allow us to always round to the nearest multiple of ten or uh depending on what you need you could put in some other some other value in here and you could mult you could round to the nearest multiple of two or five or fifteen or something else in fact it doesn't even have to be a whole number if you wanted to round to the nearest like 2.5 uh you could you could put 2.5 in there and it will still work uh we will have 87.50 that is the nearest multiple of two and a half to to this number that we that we have there so as i said uh i say the word rounding function that is really referring to any of the three rounding functions if you want to round down to the nearest multiple of 10 you could instead say floor and leave the rest of the code exactly the same and this will give you 80. if you were to instead seal this will give you 90. if you were to try to seal with the nearest 2.5 this should also give us 90 because 88.25 is closer to 90 than than it is to uh what was the other one 87.5 if you were to use the seal function you would also get 90 because again we're rounding up now depending on how often you you need to do this if you need to do this just just once or twice in your entire game it's probably fine you probably won't mind just typing out this code i'm going to change this back to round you may if you need to do this often you may need to you may desire to at least instead define some of your own functions perhaps let's call them i'll do it up here at the top uh function let's call it round x for like an extended rounding function and uh this will take key parameters this will take n which is just the number that you want to round and the number that you want to round and i guess we'll call it the base that you want to round to i'll just call this bass out of like a better things to call it and you can wrap this logic inside inside a function and you could say return round n divided by base times base and that is going to be a general form of what we just what we had previously written out manually so now i can call this function call it round x um let's uh let's round the value of n and let's go again to the nearest 10 and this will give us 90. just like that if you wanted to have a a separate function for flooring uh you could call it instead maybe perhaps floor extended and you could call you could call it like this uh if you wanted to do a seal version to round up like this uh floor x would give us 80. clx would give us 90. okay so that's advanced rounding uh if you wanted to and admittedly i there are not many situations where you would want to do this you can also do this with other mathematical operations this does not have to be restricted to round into a nearest multiple of of 10 or or anything like that if you wanted to instead round to the nearest square number um you could perhaps i'm going to just define another function uh let's call this um let's say round square and this is only going to take one parameter but if you wanted to you could return the result of round square root of n um this is the square root of n is equivalent to the stuff that was inside the rounding function before uh the rest of it is going to take a similar form and we are going to raise the results of the rounding function to the power of of two and therefore if i were to say round square and we would be rounding to the nearest to the nearest square number this will give us 81. just like this uh if i were to instead pick uh let's let's go with another number let's try it around like 75 or something like that that will also give us 81. it'll be rounded up instead of down to 81. if our if i were to instead try to round 70 to the nearest square uh we would have 64 because 64 is closer is the square number that's closest to 70 then than 81 rather than 81 rather and at this point your imagination should be starting to run wild with other things that you can do with this something that i find myself doing on occasion is rounding to the nearest power of two uh again this is going to be rather similar i can call this function round nearest power of two please think of a better name to give this if you do it yourself and instead of doing something like this instead of returning uh the rounded square root raised to the second power we are instead going to um round the uh the log of two and instead of raising that value to the second power we are going to do it in reverse we are going to raise two to the power of whatever this this value is and this is going to allow us to round 70 to the nearest power of 2 which incidentally is also going to be 64 because uh 2 to the 6 is 64. if i were to make this something more like 100 we would instead be getting 128 just like this if we were to give it a truly staggeringly gargantuan number uh that is i don't even know what number that is i want to say that's um 10 million the nearest power of two there is going to be eight million three hundred eighty eight thousand six hundred eight that is if i'm counting direct correctly two raised to the 23rd so uh things you might want to do this for if you're working with in computers you tend to work with binary numbers a lot because powers of two are special magic numbers that tend to appear over and over again for various purposes um whenever i'm working with things like trying to create texture pages on the fly using surfaces or something like that or assembling them out of existing sprites um it is helpful to uh instead of rounding to the nearest power of two maybe to instead seal to the nearest power of two and then instead of um instead of rounding up or down to the nearest power of two this will round up to the nearest power of two and if you want to create for example a texture page that will be big enough to fit the dimensions of a um of an image you might want to do something like that i do know what this is gonna be off the top of my head this is gonna be a 16 million seven seven seven thousand two hundred and sixteen um as you can see and i'm not just so much of a nerd that i've that i've memorized that this is actually a number that appears uh fairly often because this is two to the 24th so that's fancy rounding i'm not going to go into other functions that you can use this for any other mathematical operation that you could like do and then reverse such as taking the square root and raising it to a power it will also work in here for what it's worth i can't really think of reasons that you would want to do something like this perhaps if you're making something like an rpg a role-playing game and you have a um a stat growth equation that goes something like i don't know the player's level is going to be equal to the the amount of exp they have squared and you want to figure out what level they are for any given experience level you could use something like floor square you could use something like this and that will tell you what level they are based on the um based on how much experience they have even if the amount of experience they have isn't an exact uh square number any function that you can take the inverse of and then and then on invert is that a word it's a word now anyway i'm gonna stop i don't think i filled 15 minutes of air time by talking about rounding but i gave it a good effort my name is michael i like wizards and dragons and making games i try to post about two game dead videos a week so if you're interested in seeing more weird and nerdy stuff like this feel free to subscribe uh no code this time no github repository this is just this is one of my little computer science demos i don't usually put those in github but i do have a patreon if you want to contribute towards these videos being made links to that will be in all the usual places you could see your name in the credits hear yourself shout out at the end you could see a bit of a preview of my future plans that kind of thing otherwise i hope you all found this useful and i will see you all later special thanks to zenjiman edward holt posho emily coyo tusk syndra larsen grenard clovis square crow and azarel studios for supporting these videos if you want to see your name in the credits or hear yourself shout out at the end head on over to the patreon page down in the video description to join the fun
Info
Channel: DragoniteSpam
Views: 173
Rating: undefined out of 5
Keywords: game maker studio 2, game maker round, game maker floor, game maker ceil
Id: TS2AdGIN74Y
Channel Id: undefined
Length: 12min 55sec (775 seconds)
Published: Wed Dec 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.