Confusing

As mentioned in the video the concept of multiple variables with the same name waveConfig is confusing. You mentioned that you could name one a different name. I think that would have been for the best. We have a script named WaveConfig and multiple WaveConfig waveconfig variables it’s confusing in the learning process trying to guess or understand exactly which one is being used at what time.

If you think it’s confusing then it probably is and you should make an effort to make it less confusing. People can’t learn if they don’t understand what they’re doing.

2 Likes

Hi Joshua,

This approach was also applied in the old version of the course, specifically in Text101, in that example the scenario was;

Text text;

and then when the variable was used;

text.text = "Hello World!";

As you can see, lots of references to the word text.

The thing to remember is the difference between type and object(instance).

In the above, Text is the type, e.g. the data type, text is the local variable name we are using, which is of the specified type Text.

Later, in this example, we access the text property of the object, hence, text.text, (object.property).

I hope the above is of use.

On a related note, the instructors don’t frequent the forums as often as they do the Q&A on Udemy, so if you want to provide this kind of feedback you may find it more beneficial to mention it on Udemy for the specific lecture. One of the student instructors will typically reply and then escalate as necessary.

1 Like

I understand the difference between the type and local variable name. My problem is that the same variable name is used multiple times in multiple places.

If you look at the code below you will see that the variable name waveConfig is used multiple times. If i understood correctly once as a public variable and then another time as a local variable for SetWaveConfig when using this.waveConfig = waveConfig it is confusing to understand which waveConfig this is. Especially considering we have a script called WaveConfig where I believe we made WaveConfig a scriptable object.

If we renamed one to waveConfigLocal or waveConfligPublic or simply waveConfig1 I think it would be more clear correlating each waveConfig to it’s proper place

   WaveConfig waveConfig;
     List<Transform> waypoints;
    int waypointIndex = 0;

	// Use this for initialization
	void Start () {
        waypoints = waveConfig.GetWaypoints();
        transform.position = waypoints[waypointIndex].transform.position;
        
	}
	
	// Update is called once per frame
	void Update ()
    {
        Move();

    }
    public void SetWaveConfig(WaveConfig waveConfig)
    {
        this.waveConfig = waveConfig;
    }
    private void Move()
    {
        if (waypointIndex <= waypoints.Count - 1)
        {
            var targetPosition = waypoints[waypointIndex].transform.position;
            var movementThisFrame = waveConfig.GetMoveSpeed() * Time.deltaTime;
            transform.position = Vector2.MoveTowards(transform.position, targetPosition, movementThisFrame);
            if (transform.position == targetPosition)
            {
                waypointIndex++;
            }
        }

        else
        {
            Destroy(gameObject);
        }
    }
}

Hi,

Thanks for the code example, what is the name of the class this example is taken from, doesn’t look like a full script.

Popping a number on the end of the variables isn’t a good approach, as it still leaves the variable fairly meaningless, if however it was the “current” wave config that was being set then having perhaps waveConfigCurrent, currentWaveConfig or activeWave would be a little more readable.

1 Like

@Rob
here’s the class:


@thespacer14
Of course you are free to call your variables whatever you like as long as you keep the code readable for you (and for others you might want to share your code with) ^^

I think some of the points of this lecture are to

  • introduce the this keyword
  • show that it is possible to have two variables with the same name but in different scopes (one is available to the entire class, the other one is a local variable inside a method)
  • show how we can deal with these variables and distinguish between them

In Rick’s example we use the this keyword to access the variable called waveConfig at the top of the class and then we set its value to the value of the local variable, which is also called waveConfig.


Yes, it is confusing at first. But the first variable has a “this” before its name, meaning it refers to the variable at the top of the class.
Since the second variable does not have a “this”, Visual Studio tries to find a local variable with that name first (and it succeeds because we have one inside the arguments list).
If there was no “this” then you would assign the local waveConfig variable to itself, which does not make sense.


That’s why there are naming conventions. Type names (class names for example) should start with a capital letter. Variable names should start with a lowercase letter.
In this case WaveConfig refers to the WaveConfig type (that’s right, it’s a scriptable object) and waveConfig refers to the variable.

6 Likes

Great reply Banpa :slight_smile:

1 Like

Privacy & Terms