A few questions about 'WaveConfig'

I apologize for my questions that may be stupid to you, but they confuse me very much…
First of all, on the top of the Start method in EnemyPathing script , there is a variable called waveConfig. Does this variable make WaveConfig script accessible in EnemyPathing script?

public class EnemyPathing : MonoBehaviour
{
   WaveConfig waveConfig;
   List<Transform> waypoints;
   int waypointIndex = 0;

// Start is called before the first frame update
void Start()
{
    waypoints = waveConfig.GetWaypoints();//get the transforms of waypoints
    transform.position = waypoints[waypointIndex].transform.position;

    Debug.Log("Waypoint: " + waypoints.Count);
}

If so, why don’t we need to say waveConfig = FindObjectOfType() just like what Rick did in the Block Breaker part?

Secondly, in the EnemySpawner script, what is the parameter “WaveConfig waveConfig” of the SpawnAllEnemiesInWave method for? Is it for connecting SpawnAllEnemiesInWave method with “currentWave” in the StartCoroutine method in Start(), so that it can access the Wave Scriptable object? If so, how does it do that?

// Start is called before the first frame update
void Start()
{
    WaveConfig currentWave = waveConfigsList[startingWave];
    StartCoroutine(SpawnAllEnemiesInWave(currentWave));
}

private IEnumerator SpawnAllEnemiesInWave(WaveConfig waveConfig)
{
    for(int enemyCount = 0; enemyCount < waveConfig.GetNumberOfEnemies(); enemyCount++)
    {
        Instantiate(
            waveConfig.GetEnemyPrefab(),
            waveConfig.GetWaypoints()[0].transform.position,
            Quaternion.identity);
        yield return new WaitForSeconds(waveConfig.GetTimeBetweenSpawns());
    }
}

Thirdly, in the EnemyPathing script, what does this.waveConfig = waveConfig change? I just do not understand what it does and why we have to do that…

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

Fourthly, in the EnemySpawner script, when we say newEnemy.GetComponent().SetWaveConfig(waveConfig), why is it “waveConfig” within the Parentheses?

 private IEnumerator SpawnAllEnemiesInWave(WaveConfig waveConfig)
 {
     for (int enemyCount = 0; enemyCount < waveConfig.GetNumberOfEnemies(); enemyCount++)
    {
        var newEnemy = Instantiate(
            waveConfig.GetEnemyPrefab(),
            waveConfig.GetWaypoints()[0].transform.position,
            Quaternion.identity);
        newEnemy.GetComponent<EnemyPathing>().SetWaveConfig(waveConfig);
        yield return new WaitForSeconds(waveConfig.GetTimeBetweenSpawns());
    }
}

Thanks a lot!

Hi!

1.- Does this variable make WaveConfig script accessible in EnemyPathing script?
Yes, and no. It’s an empty variable, you have to set it and it’s been set in this method:

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

This method is being called in the EnemySpawner. Check the method SpawnAllEnemiesInWave and search for this line:

newEnemy.GetComponent<EnemyPathing>().SetWaveConfig(waveConfig);

This means every time a new enemy is spawned the EnemySpawner sets the waveConfig variable for them. This also answers your fourth question.

2.- What is the parameter “WaveConfig waveConfig” of the SpawnAllEnemiesInWave method for?
What you are doing there is setting a local variable that the Coroutine needs for it to run, in other words you are doing this:

theWaveConfigSpawnNeeds = aParticularWaveConfig;
//or
waveConfig = currentWave;

You are pretty much declaring a variable.

3.- What does this.waveConfig = waveConfig change?
This is needed because you have two variables with the same name but one is Global (a variable that can be accessed by any method within that particular class) and the other is Local (a variable than can only by accessed within the method it was declared).

 WaveConfig waveConfig; //<--- this.waveConfig Global.
public void SetWaveConfig(WaveConfig waveConfig) //<--- waveConfig. Local.
{
    this.waveConfig = waveConfig;
}

4.- Why is it “waveConfig” within the Parentheses?
Because it is calling the method SetWaveConfig (which has a waveConfig parameter so it needs it) from each enemy spawned so the enemy can know how fast it can move.

  1. I don’t quite understand your answer…what I meant was what the WaveConfig waveConfig does on the very top in the EnemyPathing script.
    image

  2. Why don’t we just use other name for one of the variables? Will it work?

1.- I think you are confusing yourself at this point, so I won’t be able to help you at all unless you tell me this; Try to explain what is going on in that script. Then I’ll be able to get where your confusion is coming from, otherwise I won’t be able to explain, because the reason why WaveConfig is at the top of the script is the same as to why the other variables (List waypoints, int waypointIndex) are also at the top of the script and I don’t see you asking about those, so please explain why you are so confused.

2.- Yes, it’s just a variable like an int, bool or any other type, you can name it whatever you like, but it’s just normal convention to name things according to what they are.

What I would recommend is to go back in the course and check this particular lessons:

  • Section 2 - 23. Unity Scriptable Objects
  • Section 2 - 25. Public Methods and Return Types
  • Section 2 - 27. Manage Next States (Here Rick does pretty much the same you are doing here)

Edit:
After analyzing the Git and re-reading your original question I think I’m getting your confusion. Are you confused because we have Waveconfig waveconfig at the top of script but also as a parameter in the method “public void SetWaveConfig(WaveConfig waveConfig)”?

If that’s the case the answer is pretty simple, they are not the same, that’s why you have to use “this.waveConfig”, to indicate that you refer to the waveConfig that is at the top of the script, actually try this, change the name of the “WaveConfig waveconfig” parameter, something like this:

 public void SetWaveConfig(WaveConfig waveConfigSent)
    {
        waveConfig = waveConfigSent;
    }

You’ll notice that it doesn’t affect your script at all and now you can remove the “this” indicator.

I know Waveconfig waveconfig at the top of script is not the same as a parameter in the method “public void SetWaveConfig(WaveConfig waveConfig)”. I just don’t know what is the use of the variable Waveconfig waveconfig at the top of script…

You are using it inside the move method.

private void Move()
 {
    if (waypointIndex <= waypoints.Count - 1)
    {
       var targetPosition = waypoints[waypointIndex].transform.position;
       var movementThisFrame = waveConfig.GetMoveSpeed() * Time.deltaTime; //<-- Here

Yes, that’s exactly what I meant. At the top of script, I say Waveconfig waveconfig, so that inside the Move method, I can use the GetMoveSpeed method from Waveconfig script, right? What I do not understand is that why I can still use it when I didn’t say waveconfig = FindObjectOfType<>() or waveconfig = GetComponent<>() withing the Start method, just like what Rick did in the Block Breaker section.


(script from Block Breaker)

Yes, you need it to access the data and that’s what you do in the Move method, you can access it because you declared it in the SetWaveConfig method which is called in the SpawnAllEnemiesInWave coroutine in the EnemySpawner script.

Also, WaveConfig is not a script, is a scriptable object, you cannot attach it to an object in the scene as a component/script, therefore you can’t look for it in the way you would look for a script.

[CreateAssetMenu(menuName = "Enemy Wave Config")]
public class WaveConfig : ScriptableObject //<-- Here
1 Like

Thank you! That cleared up confusion.

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

Privacy & Terms