Pretty much everything in your game at some point requires calulations, and or basic arithmetic to function as a game.
The math library just provides you to just the basic tools for accomplishing this task, if you need a random number, ask Random.new() or math.random to give that number using a pseudo-random number generator, if you to round a number to appear more simplified, while still being approximate to a specific value, ceilfloor and round are there, if you need to get the distance from 0, abs is there.
There isn’t a specific use for every program, its just a tool for when you need it.
math.random is very useful for adding variation even if it’s on little things such as cooldowns picking random values and etc math.round just brings you to the closest interger which can avoid stuff like infinite punctuation math.abs i don’t actually have an example for lol
Is what I find important to me. What it does if the number goes over the maximum amount it will return the max amount, and same thing with minimum. If it’s goes below the minimum then it will return the minimum count.
I have tried using math.clamp for a bit. For me, it’s similar to rounding a number to another variable.
What can it be used for? I tried to use math.clamp in the past but haven’t seen it’s point.
the code above will set the currenthp to 100 because the healamount is 250 which is greater than the limit;
im really bad at explaining but i hope this clears it up for you
There is a good alternative to math.random called Random.new(). It’s a lot more flexible and supports floating point values (no more having to do math.random(10, 100) / 100).
math.abs at its core is actually really simple. If x is less than 0, invert it.
You can even make your own abs function if you really wanted too.
function abs(x)
if x < 0 then
return -x
end
end
I don’t know much cases where you would always want a number to be positive, but its there if you need it.