Search Unity

Problem with spawning rooms

Discussion in 'Scripting' started by ProExCurator, Jan 13, 2020.

  1. ProExCurator

    ProExCurator

    Joined:
    Jan 2, 2020
    Posts:
    87
    Hi guys

    I'm trying to recreate this:


    This is how it looks like:
    https://pastebin.com/BZe5jRL3
    https://pastebin.com/2qsHd7xk

    The problem is that the "end room" is spawning in the same place where the "start room" is, but it shouldn't of course and I don't understand why, because it looks like everything is the same as in the video.

     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,736
    Some steps to debug:

    - locate where it spawns the room in the code
    - find out where it computes the position
    - make sure that computed position is reasonable
    - make sure that computed position is assigned to the newly-spawned room.

    Use
    Debug.Log();
    to track things down, or else attach the debugger and single-step it, your choice really.
     
  3. ProExCurator

    ProExCurator

    Joined:
    Jan 2, 2020
    Posts:
    87
    So I was looking for the reason for some time and I found out that the whole project is on github. So I copied scripts for generation to check if I made mistake somewhere and... I turns out that I didn't, because now everything is exactly how it is there and it still doesn't work. So I really don't understand why it is not working?

    https://github.com/chillehh/Binding-of-Isaac-Tutorial
     
    Last edited: Jan 14, 2020
  4. ProExCurator

    ProExCurator

    Joined:
    Jan 2, 2020
    Posts:
    87
    I still didn't solve this problem.

    I specified what is responsible for spawning "end room" (room with boss) in code:

    DungeonGenerator
    Code (CSharp):
    1. public class DungeonGenerator : MonoBehaviour
    2. {
    3.     public DungeonGenerationData dungeonGenerationData;
    4.     private List<Vector2Int> dungeonRooms;
    5.  
    6.     private void Start()
    7.     {
    8.         dungeonRooms = DungeonCrawlerController.GenerateDungeon(dungeonGenerationData);
    9.         SpawnRooms(dungeonRooms);
    10.     }
    11.  
    12.     private void SpawnRooms(IEnumerable<Vector2Int> rooms)
    13.     {
    14.         RoomController.instance.LoadRoom("Start", 0, 0);
    15.         foreach(Vector2Int roomLocation in rooms)
    16.         {
    17.             RoomController.instance.LoadRoom("Empty", roomLocation.x, roomLocation.y);
    18.         }
    19.     }
    20. }

    DungeonCrawlerController:

    Code (CSharp):
    1.     ...
    2.     public static List<Vector2Int> GenerateDungeon(DungeonGenerationData dungeonData)
    3.         {
    4.             List<DungeonCrawler> dungeonCrawlers = new List<DungeonCrawler>();
    5.  
    6.             for(int i = 0; i < dungeonData.numberOfCrawlers; i++)
    7.             {
    8.                 dungeonCrawlers.Add(new DungeonCrawler(Vector2Int.zero));
    9.             }
    10.  
    11.             int iterations = Random.Range(dungeonData.iterationMin, dungeonData.iterationMax);
    12.  
    13.             for(int i = 0; i < iterations; i++)
    14.             {
    15.                 foreach(DungeonCrawler dungeonCrawler in dungeonCrawlers)
    16.                 {
    17.                     Vector2Int newPos = dungeonCrawler.Move(directionMovementMap);
    18.                     positionsVisited.Add(newPos);
    19.                 }
    20.             }
    21.  
    22.             return positionsVisited;
    23.         }
    24.     }
    RoomController - Here the boss room is spawned
    Code (CSharp):
    1.     public class RoomInfo
    2.     {
    3.         public string name;
    4.         public int X;
    5.         public int Y;
    6.     }
    7.  
    8.     public class RoomController : MonoBehaviour
    9.     {
    10.  
    11.         public static RoomController instance;
    12.  
    13.         string currentWorldName = "Basement";
    14.  
    15.         RoomInfo currentLoadRoomData;
    16.  
    17.         Room currRoom;
    18.  
    19.         Queue<RoomInfo> loadRoomQueue = new Queue<RoomInfo>();
    20.  
    21.         public List<Room> loadedRooms = new List<Room>();
    22.  
    23.         bool isLoadingRoom = false;
    24.         bool spawnedBossRoom = false;
    25.         bool updatedRooms = false;
    26.  
    27.     ...
    28.  
    29.     IEnumerator SpawnBossRoom()
    30.         {
    31.             spawnedBossRoom = true;
    32.             yield return new WaitForSeconds(0.5f);
    33.             if(loadRoomQueue.Count == 0)
    34.             {
    35.                 Room bossRoom = loadedRooms[loadedRooms.Count - 1];
    36.                 GameObject go = new GameObject();
    37.                 Room tempRoom = go.AddComponent<Room>();
    38.                 Destroy(bossRoom.gameObject);
    39.                 var roomToRemove = loadedRooms.Single(r => r.X == tempRoom.X && r.Y == tempRoom.Y);
    40.                 loadedRooms.Remove(roomToRemove);
    41.              LoadRoom("End", tempRoom.X, tempRoom.Y);
    42.             }
    43.         }
    44.  
    45.         public void LoadRoom( string name, int x, int y)
    46.         {
    47.             if(DoesRoomExist(x, y) == true)
    48.             {
    49.                 return;
    50.             }
    51.  
    52.             RoomInfo newRoomData = new RoomInfo();
    53.             newRoomData.name = name;
    54.             newRoomData.X = x;
    55.             newRoomData.Y = y;
    56.  
    57.             loadRoomQueue.Enqueue(newRoomData);
    58.         }
    59.  
    60.     ...


    Is someone able to help me?
     
    Last edited: Jan 16, 2020
  5. ProExCurator

    ProExCurator

    Joined:
    Jan 2, 2020
    Posts:
    87
    Can someone help please. Still don't know where the problem is...
     
  6. ahapke

    ahapke

    Joined:
    Jan 19, 2018
    Posts:
    1
    Hi there!
    I know it's way too late but in case anyone else has this problem:
    I had the same issue and at one point I realised that the end room spawned at the right place and only the picture at the start room. You could check if the picture is in the room or outside in the scene. That was my issue. Other than that, the guy has a script somewhere where he fixes the double-spawning problem.
    Hope that helped!