Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Waves System for a Tower Defense

Discussion in 'Scripting' started by Padrox_, Dec 26, 2022.

  1. Padrox_

    Padrox_

    Joined:
    Jun 12, 2021
    Posts:
    15
    Hello,

    I'm currently making a Tower Defense game and I'm working on the Waves System.

    How does it work?

    Each Wave contains a List of SubWaves.
    Subwaves are a struct with the EnemyToSpawn, how many, on which path (because my game handles multiple paths), the delay before starting spawning the subgroup, and the interval between each spawn.

    It looks like that:
    Code (CSharp):
    1.     public struct SubWave
    2.     {
    3.         public MovementController EnemyToSpawn;
    4.         public int NumberToSpawn;
    5.         public float TimeBetweenSpawns;
    6.         public float SpawnTimestamp;
    7.         public PathCreator Path;
    8.     }
    So a wave can have X subgroups that can spawn simultaneously.
    For exemple:
    - spawn 8x Goblin every 1 seconds on Path 1 as soon as the wave begins
    - spawn 3x Orc every 3 seconds on Path 2 as soon as the wave begins
    - spawn 4x Skeleton every 2 seconds on Path 1 after 10 seconds of the wave has passed.

    I can then consider the wave as cleared when all the enemies of this wave have been killed and every subgroups are done spawning.

    The issue I'm encountering is that I need to run the subgroups asynchronously.
    What is the best solution for me to achive this?

    I stay tuned if you need any further explanation of my issue,
    Thanks in advance,
    Padrox.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    Why not just define ScriptableObjects for each wave? Then you can drag them all into a collection (Array or List), and even that could be in another kind of ScriptableObject.

    Iterating them could be as simple as making a "wave runner" kind of object that iterates through that list, giving each wave followed by a specified delay. The delay could be part of the wave definition or else it could just be another kind of wave object that only specifies a delay and produces zero enemies itself, which lets you layer or mix and match them however you like.
     
  3. Padrox_

    Padrox_

    Joined:
    Jun 12, 2021
    Posts:
    15
    Thank you for your fast answer,

    First, I already have a ScriptableObject called WavesData.
    Each Map / Level will have its own assigned WavesData.
    It looks like that in the Inspector:
    upload_2022-12-26_17-13-31.png

    I'm currently iterating through each wave and then through each subgroup of that wave.
    But I don't know if multi-threading, jobs or something like that could suit my case.
    Or if I can find an alternative solution to spawn every subgroups at the same time.
     

    Attached Files:

  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    Oh, no, don't reach for that. Way overkill.

    Just a simple integer to advance along the collection would be fine, combined with the "wave yet" timer.

    Code (csharp):
    1. private int index;
    2. private float timer;
    In Start():

    Code (csharp):
    1. index = -1;
    2. timer = 0;
    Every Update():

    - count down the wave yet timer (
    timer -= Time.deltaTime
    );
    - if the wave yet timer is zero or less:
    ---> increment the index (which you started at -1)
    ---> if there are no more waves then you're done
    ---> look up the spawn using the index
    ---> Spawn the wave
    ---> Read the time to wait into the wave yet timer

    That's it.
     
    Last edited: Dec 26, 2022