I don't understand a method

Hey!
I apologize in advance for my English, because it isn’t good. I believe you will understand me.
I have a problem with understanding this method:

    public void SetWaveConfig(WaveConfig waveConfig)
    {
        this.waveConfig = waveConfig;
    }

I don’t know if I’m right, but each waveConfig in this class after “this.” is replaced by waveConfig after equal sign, but I don’t understand what it gives. What does this change? I know I might be wrong and I accept it. Maybe I didn’t quite understand what Rick said because of my English, but I didn’t have major problems before. I believe you can help me!

1 Like

Hi DemoniX,

I think you are right. However, just to be sure, let me explain something:

waveConfig is not an object. It’s a variable. A variable is a container for data, here: a reference (“link”) to an object of type WaveConfig, or nothing (null).

this.waveConfig refers to the instance variable at the top of your code. The other waveConfig is the local variable of the SetWaveConfig method of this instance.

You wrote that waveConfig replaces this.waveConfig. That is almost correct. Not the variable gets replaced but the value of the variable.

If this.waveConfig references WaveConfig object A, and waveConfig references WaveConfig object B. After this.waveConfig = waveConfig got executed, this.waveConfig references WaveConfig object B, not object A anymore.

This behaviour is the reason why types such as WaveConfig are called reference types.

If this is what you meant, you understood everything correctly. :slight_smile:

1 Like

Okay, I understand what you wrote, thank you! I appreciate it!. But still I have another problem. We have on the top of our class a variable called waveConfig. What does exchange in this method give us in this case? I will show you this code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyPath : MonoBehaviour
{
    WaveConfig waveConfig;
    List<Transform> waypoints;
    int waypointIndex = 0;
     
    // Start is called before the first frame update
    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 targetPosision = waypoints[waypointIndex].transform.position;
            var movementThisFrame = waveConfig.GetMoveSpeed() * Time.deltaTime;
            transform.position = Vector2.MoveTowards(transform.position, targetPosision, movementThisFrame);

            if (transform.position == targetPosision)
            {
                waypointIndex++;
            }
        }
        else
        {
            Destroy(gameObject);
        }
    }
}

I don’t understand what the purpose of this method is :frowning: .

The waveConfig method at the top of your code is private. This means that nothing but the instance itself can access it. This is good practice because it makes your code safer.

The purpose of the SetWaveConfig method is to receive a WaveConfig object and to assign it to the waveConfig variable at the top of our code.

Why not doing that directly? Why a method?

Because you might want to check the object at a later juncture before you assign it to the variable. You have the control over the waveConfig variable at the top of your code with this method. The more complex your project becomes, the more important having control over as much as possible is. Otherwise, you’ll spend a lot of time looking for errors when errors pop up because they could come from everywhere.

And making our object more secure, is the reason why we created this method.

Does that make sense?

1 Like

Oh my god, it does! I finally understood it! Thank you very much! :heart:

1 Like

That’s great to hear. I’m glad I was able to help. :slight_smile:

1 Like

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

Privacy & Terms