Routine parameters

Is there a reason for passing the 4 parameters to the FadeRoutines?

The passed in values are class references anyway (or initialisible before the while loop). Am I missing something subtle, or is it done to highlight the technique of overloading methods?

It’s good practice. If these parameters were set in the method, there would be no way to change it. For example, targetTransparency is currently set to 1 when exiting the trigger and 0.8 when entering it. You need to pass it in. The same goes for whether or not it’s the TileMap or the SpriteRenderer. By passing the values we are ‘injecting’ the dependencies. An argument could be made for fadeTime and startValue but that would restrict our usage of it.

A simple example would be

int Sum()
{
    return 2 + 3;
}

These values are in the method and there is nothing I can do to get this method to give me the sum of 5 and 7

2 Likes

Thank you.

So, in effect, in the course code the fact that the parameter names and the class reference names are the same is irrelevant. We are looking at two distinctly different references (one class, one local - as an aside, the reason why we often see this.variable = variable).

And the fact we are (currently) ONLY passing in the variables of the same name is irrelevant, we could (at a later date) call the method again from a different source, passing in completely different variables for a different result.

Gotcha!

Oh, yes. The only thing that’s relevant really is the type. You could change the parameter names and the call will still work exactly the same. If, for the sake of example, you had 2 SpriteRenderer objects, you could pass any one of them in. Regardless of what they are called

1 Like

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

Privacy & Terms