- Home /
Is there a good reason the Unity 2D platformer tutorial code is like this?
In the Lerpz 2D platforming tutorial there is a PlatformerController script that calculates movement and jumping. A variable inside the ApplyJumping function calls to another function to get the result…
function ApplyJumping () {
movement.verticalSpeed = CalculateJumpVerticalSpeed (jump.height);
}
function CalculateJumpVerticalSpeed (targetJumpHeight : float) {
return Mathf.Sqrt (2 * targetJumpHeight * movement.gravity);
}
I'm trying to learn as many efficient coding techniques as I can so I'm wondering if there's a reason the programmer chose to put this into its own function? Why didn't he just write it as:
function ApplyJumping () {
movement.verticalSpeed = Mathf.Sqrt (2 * jump.height * movement.gravity);
}
…because to me, this seems more efficient. Why did he create a whole function to call to? Was it unnecessary or is there a reason for it? The CalculateJumpVerticalSpeed function is not called anywhere else in the project.
//edit: might sound like I'm criticizing but I'm not! Just trying to learn. :)
did you try commenting out the original code and putting your version into play to see if it made any kind of difference?
Answer by syclamoth · Dec 13, 2011 at 11:01 PM
It doesn't make any difference. The reason is good programming technique. While the function is currently not called anywhere else in the project, it might be at some point in the future- it's always best to split this kind of thing down into small, modular operations so that it's easy to see exactly what parts do what. For example, you might want to make some kind of 'jump height calculator' (for AI use, or to give the player a feel for how high they will jump) which uses the same function. Instead of copy-pasting the code from the jumping part, just make another call to the 'calculate jump height' function! This way, if you make a change to one, the change propagates to the other without you having to do any work- since they are both using the same line of code.
comment it out, use yours, and see if it has an effect on performance or if it all runs the same.
Yes, it's a small overhead but the code is much cleaner and easier to follow. And since it's a tutorial, the code should be easy to follow and not contain a lot of "WTF" moments for the reader. I regulary break down code into small methods like that.
I just realized the audience might be going "WTF" for different reasons.
@syclamoth, apparently it doesn't inline methods that return float.
The number is the amount of thumbs up that post has been given. It's a small measure of how much people like the comment.
Whether the compiler optimizes it out or not the amount of overhead is negligible. It's really unlikely that you will ever have that amount of optimization make a difference in the performance of your system working in Unity.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Call function from other script 0 Answers
Where can i find tutorial for unity free Not unity pro ? 2 Answers
Saving data. 1 Answer
Multiplayer FPS Tutorial 1 Answer