I am trying to make sure I understand what, why and when I should use the functions explored in the course. Case in point, the abs() function. Is it used solely to make sure a positive value is returned? Could you give me further examples of why and/or when a guaranteed positive value would be important?
Likewise, why round X and Y components to 0 or 1? What happens if you don't?
I am doing my own research on the side, of course, as it is part of the learning, but often folks explain things as if they are talking to other very experienced programmers. The advantage of asking here is that I can ask you all to be free to dumb down the answer if necessary. Explain it like I am a barely functional 2 years old.
Thank you!
Asking for clarification is never a bad thing! We all learn differently, and I know for a fact that I occasionally trip up over unexplained side-details all the time, and other times complex matters just click.
For the abs() function, a good example is knowing the distance between objects. In a coordinate system, objects can go negative (think -1, -1 on a graph). Assuming identical Y values - if a player is at -100px, and an enemy is "searching" for a target at -150px, the distance between those objects is 50px, positive 50px. We can then use that 50px value to determine if the enemy can "see" the player, or whatever else we want to do. We don't care where in world space those objects are, we care how close they are to each other - in this case, the absolute value gives us that distance, regardless of where we are on a coordinate system.
As for rounding components to 0 or 1, I can't say off hand, but the best way to figure out is to experiment! Try temporarily removing the rounding to see what happens, you can always add it back. Without any context (I don't currently have the class), I'd imagine something to do with smoothing movement, or removing undesired effects from working with decimals (stutter, inaccurate values, etc), but I can't / won't say without seeing it and being sure.
It's worth noting I didn't change a thing about my explanation, this is just how I communicate! Hope this helps, and feel free to ask if you have any more questions.
Is it used solely to make sure a positive value is returned? Could you give me further examples of why and/or when a guaranteed positive value would be important?
Yes, it is used to turn negative numbers into positive ones. As mentioned by GGemfruit, you typically use this function to calculate the distance between two numbers (two int or two float values), to make comparisons.
It is actually hard to give you many very concrete examples. Especially in a context of Godot, this function is most often useful when coding algorithms or shaders, which require more math code than your typical game.
Likewise, why round X and Y components to 0 or 1? What happens if you don't?
Our direction vector can have an infinite number of value combinations. the x and y components can have values like 0.5738618. But our dictionary has very precise key value pairs and to get a given value we must provide the unique corresponding key. Rounding the numbers and using abs() in conjunction reduces the possible values from our infinite variety of direction vectors to only five possibilities.
Now here the most important technique and takeaway here is using a dictionary with a finite number of key-value pairs somewhat creatively. The "frame lookup" technique as we call it. It may not always require to call both round() and abs(), depending on the exact game you're making. But it's a powerful trick to get used to. As you gain experience, you'll get to see that dictionaries can sometimes greatly simplify game code.