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. Dismiss Notice

Check if int list has duplicates, and remove 1 from each of those at a time

Discussion in 'Scripting' started by thevvulf, Aug 29, 2021.

  1. thevvulf

    thevvulf

    Joined:
    Oct 31, 2020
    Posts:
    36
    I tried to explain it in my title, but its a weird situation. Basically I have 5 elevators, each of them can spawn enemies. I have a thing to randomize the amount of enemies based on the wave count. Then, I loop over each value and assign it a random elevator that it should spawn in. The thing is, since I'm almost always going to be spawning more than 5 elevators, I need them to be spawned with slight delays. For example, say 3 enemies spawn at elevator 2 and 2 enemies spawn at elevator 5. The spawn pattern would go like this: Spawn one at 2 and 5 then wait, spawn one at 2 and 5 then wait, spawn one at 2. This way, I can spawn every enemy, but they won't be spawned on top of each other. I looked up multiple solutions, and what got me the closest was using a list, and if the value of the index was greater than 0, subtract one from the value as well as spawn an enemy. Here's what that looks like:
    Code (CSharp):
    1. void SpawnWave(int waveCount)
    2.     {
    3.         int enemyCount = Mathf.RoundToInt(Random.Range(waveCount, (waveCount + 1) + waveCount * 0.2f));
    4.        
    5.         for (int i = 0; i < enemyCount; i++)
    6.         {
    7.             int spawnElevator = Mathf.RoundToInt(Random.Range(1, 5));
    8.             elevatorSpawnedLocations.Add(spawnElevator);
    9.         }
    10.         for(int i = 0; i < elevatorSpawnedLocations.Count; i++){
    11.             int tempVal = elevatorSpawnedLocations[i] - 1;
    12.             if(tempVal > 0){
    13.                 elevatorSpawnedLocations[i] = tempVal;
    14.                 Object.Instantiate(enemy1Prefab, elevators[elevatorSpawnedLocations[i]].transform.position, Quaternion.Euler(0,-90, 0));
    15.             }
    16.         }
    17.     }
    I think it works, but I don't know how I would tell if it did or not. I don't know how to implement the spawn delay, and I can't get my brain to figure it out for some reason. I've been staring at vs code mindlessly for 30 minutes and nothing has worked. Does anyone know if this would work in the first place, and if it should/does work, how should I implement the delay, so they aren't on top of each other? Sorry if its a terrible question, I'm having a complete brain fart right now and can't think of a solution for the life of me.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    The problem you describe and your post title, I don't feel like really match up. But, if you are simply trying to loop through all your elevators, spawn one enemy, and then pause and then loop through, spawn one enemy, and then pause. This is pretty easy to do.

    Now, the basic solution is simple.

    For a timer you have a few choices. There are examples of using Update with a variable that increments by adding a value such as Time.DeltaTime to it and checking when it passes a preset value and that determines you should do something. Then you adjust the value and keep going.

    Another way is with a coroutine that lets you use a yield wait statement. That way you can spawn and wait and then let it loop and spawn and wait again.

    Now, as far as the number of enemies to spawn. I would probably use a dictionary and add each Elevator # as a key and the value being the number of enemies to spawn. Then I would subtract one from the count for that elevator as I spawned it. If I hit zero, I would either remove the key or just leave it and just continue to no longer spawn enemies.

    Note this isn't the only way to do it, just a quick 2 mins of thought on how I might set something like this up.
     
  3. thevvulf

    thevvulf

    Joined:
    Oct 31, 2020
    Posts:
    36
    I can't just spawn one at a time in each elevator, since you might have different amounts of enemies at each elevator. Like in my example, say I have 2 enemies at elevator 5 and 3 enemies at elevator 4. i would take one from each, bringing it down to 1 and 2 enemies. Then do that again, bringing it down to only 1 enemy left at the 4th elevator, and none left at the 5th. Then I spawn the last enemy in the 4th elevator. This is what I meant by my title. If any of the elevators have more than one enemy (thats what I meant by duplicates, since each enemy has a different elevator index, duplicate would mean you have duplicate indexes), you take one enemy from the amount of enemies left and spawn one at the same time, until each elevator has no enemies left to spawn.
    I know its really confusing, which is probably why I haven't thought of anything in an hour.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Ok, well, either way, my solution still does what you want. Unless I'm still not understanding.

    Elevator 1 3 enemies
    Elevator 2 5 enemies
    Elevator 3 1 enemy

    Enter timer loop
    Spawn
    E1 - 1 enemy, subtract 1 from E1 count (now 2 left)
    E2 - 1 enemy, subtract 1 from E2 count (now 4 left)
    E3 - 1 enemy, subtract 1 from E3 count (now 0 left)
    Pause (coroutine or Update timer)
    Spawn
    E1 - 1 enemy, subtract 1 from E1 count (now 1 left)
    E2 - 1 enemy, subtract 1 from E2 count (now 3 left)
    E3 - 0 enemy as there are none left to spawn
    Pause (coroutine or Update timer)
    Spawn
    E1 - 1 enemy, subtract 1 from E1 count (now 0 left)
    E2 - 1 enemy, subtract 1 from E2 count (now 2 left)
    E3 - 0 enemy as there are none left to spawn
    Pause (coroutine or Update timer)
    Spawn
    E1 - 0 enemy as there are none left to spawn
    E2 - 1 enemy, subtract 1 from E2 count (now 1 left)
    E3 - 0 enemy as there are none left to spawn
    Pause (coroutine or Update timer)
     
  5. gjaccieczo

    gjaccieczo

    Joined:
    Jun 30, 2021
    Posts:
    306