Project Boost: Cleaning code here is confusing

Alright, so the code should look sth. like this:

Just to be clear. Calling ApplyRotation(rotationThrust); we want to use the method ApplyRotation using the parameter rotationThrust, which we set to 100 above, so basically ApplyRotation(100).

Now the method is declared as void ApplyRotation(float rotationThisFrame), which means we are basically saying to set rotationThisFrame to 100, like: 100 = rotationThrust = rotationThisFrame.

Is there a reason we cant get rid of one of these variables? Or am I missing an important aspect?
Wouldn’t it be easier to just leave the transform.Rotate… in the if statement?

void ProcessRotation()

{

    if (Input.GetKey(KeyCode.A))

    {

        ApplyRotation(rotationThrust);

    }

    else if (Input.GetKey(KeyCode.D))

    {

        ApplyRotation(-rotationThrust);

    }

}

void ApplyRotation(float rotationThisFrame)

{

    transform.Rotate(Vector3.forward * rotationThisFrame * Time.deltaTime);

}

Hi Patrick,

Why would you want to get rid of the variables? Did you test your idea?

I did not test it yet.
I Just did not get, why we have rotationThrust and rotationThisFrame.

You could get rid of the parameter and use rotationThrust instead.

If you are more experienced with coding, one thing you might need to remember is that the course is designed with beginners in mind, so the code might not be the most efficient nor the cleanest, sometimes it’s just an excuse to teach the student something new, like in this case Rick is showing encapsulation. This will happen across all the sections, keep that in mind.

Yee is right. Rick very likely named the parameter of the ApplyRotation method rotationThisFrame to avoid any misunderstandings. The parameter is a local variable of the ApplyRotation method. We could name it rotationThrust. That does not matter.

If we do the following, we pass on a value, not a variable:

ApplyRotation(-rotationThrust);
ApplyRotation(blahblah);
ApplyRotation(new Vector3(1f, 1f, 1f));

We pass on a value to the method to make the method as independent of things outside its code block as possible. That’s considered good practice in object-oriented programming.

Of course, you could drop the parameter and have the ApplyRotation access the instance variables.

Mmmh, I am still not sure, if I understand what we were doing in this section.

So basically ApplyRotation(blahblahblah) means: Do the method called ApplyRotation, using the variable blahblahblah?

And the method would be declared like
void ApplyRotation (float notblahblahblah) and then the variable blahblahblah will be “transfered” to the variable notblahblahblah to use in that code?

Did I sum it up correctly?

The value of the blahblahblah variable.

void ApplyRotation (float notblahblahblah) and then the variable blahblahblah will be “transfered” to the variable notblahblahblah to use in that code?

Exactly. If you call ApplyRoation(XYZ);, the value of XYZ gets stored in notblahblahblah. The value of XYZ could be (1f, 2f, 3f) or (99f, 0.003f, 13234234f) or something else. It does not matter.

Then you can use notblahblahblah in your code block without having to know what the actual value will be. notblahblahblah is like a placeholder for the actual value which you do not know while typing the code.


See also:

I think I get it now. I will probably see some more examples in the future, so that I will remember that. Thank you!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms