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

Question Printing 2D Array

Discussion in 'Scripting' started by Wibaut, Jan 25, 2021.

  1. Wibaut

    Wibaut

    Joined:
    Jan 22, 2021
    Posts:
    5
    I've been trying to learn how to make a randomly generated level and want to check if what I put into my 2D array is what I actually want.

    so far I wrote this, I'm trying to print my 2D Array by putting it in a string and printing that, then I paste it in Notepad and just hit enter every roomSizeX (100) and see if I get the desired result, multiple blocks surrounded by nothing. it should look something like this:



    but I am getting this

    Screenshot 2021-01-25 094327.png

    I tried multiple times, but it always is a long line of 0 going all the way up on the right side (most of it is cut out in the screenshot) and a line at the bottom.

    Is this due to how I print this, or did I make a mistake with creating this 2D Array?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DungeonCreator : MonoBehaviour
    6. {
    7.  
    8.     [SerializeField] int mapSizeX, mapSizeY, roomAmount;
    9.  
    10.     void Start()
    11.     {
    12.         string result = "List contents: ";
    13.         foreach (var item in CreateRooms())
    14.         {
    15.             result += item.ToString();
    16.         }
    17.         Debug.Log(result);
    18.     }
    19.  
    20.     private int[,] CreateRooms()
    21.     {
    22.  
    23.         int[,] newMap = new int[mapSizeX, mapSizeY];
    24.  
    25.         for (int i = 0; i < roomAmount; i++)
    26.         {
    27.             int roomSizeX = UnityEngine.Random.Range(10, mapSizeX / 5);
    28.             int roomSizeY = UnityEngine.Random.Range(10, mapSizeY / 5);
    29.             int roomPosX = UnityEngine.Random.Range(1, mapSizeX - roomSizeX);
    30.             int roomPosY = UnityEngine.Random.Range(1, mapSizeY - roomSizeY);
    31.  
    32.             for (int x = 0; x < roomPosX + roomSizeX; x++)
    33.             {
    34.                 for (int y = 0; y < roomPosY + roomSizeY; y++)
    35.                 {
    36.                     newMap[x, y] = 1;
    37.                 }
    38.             }
    39.         }
    40.  
    41.         return newMap;
    42.     }
    43.  
    44. }
    45.  
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    On lines 32-34, since you start your loops at 0, every room is going to start in the (0,0) corner and extend out from there. (You probably wanted to start those loops at roomPosX and roomPosY. You also probably want the random ranges for roomPosX and roomPosY to start at 0 rather than 1.)

    However, the rooms still get as close to the opposite edges as they would have anyway--this error extends them to the (0,0) corner, but it doesn't move them. So you would have had just as much 0 space on the high end anyway; it's just that you might have had a symmetrical space on the low end.

    Have you considered adding line-breaks to your output string so that you don't need to add them manually?
     
  3. Wibaut

    Wibaut

    Joined:
    Jan 22, 2021
    Posts:
    5
    Yeah I was thinking about adding line-breaks, but I had no idea how, so I just went with the horrible way of hitting the enter key 100 times for each test.
    how would I a linebreak every n characters?

    So setting x to roomPosX and y to roomPosY would be the fix? dont really feel like testing it again, before I figure out the linebreak bit hahaha.

    Code (CSharp):
    1.             for (int x = roomPosX; x < roomPosX + roomSizeX; x++)
    2.             {
    3.                 for (int y = roomPosY; y < roomPosY + roomSizeY; y++)
    4.                 {
    5.                     newMap[x, y] = 1;
    6.                 }
    7.             }
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Something like
    Code (CSharp):
    1. int[,] map = CreateRooms();
    2. for (int y = 0; y < mapSizeY; ++y)
    3. {
    4.     for (int x = 0; x < mapSizeX; ++x)
    5.     {
    6.         result += map[x,y];
    7.     }
    8.     result += "\n";   // line break
    9. }
    You may want to change the order of iteration depending on how you imagine your axes being laid out in physical space (this will have the origin in the top-left, with X increasing to the right and Y increasing as you go down).

    Also if you were going to be doing this a lot during an actual game you should use a StringBuilder to reduce the number of memory allocations, but for a debugging function that doesn't especially matter.