Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How do i put multiple values in a single list value?

Discussion in 'Scripting' started by ShokTheIV, Dec 24, 2020.

  1. ShokTheIV

    ShokTheIV

    Joined:
    Nov 19, 2020
    Posts:
    17
    U c im in a particular situation where my pc is broken so im programming with me older laptop and the latest version it can handle is 2019.2.16f so ik i have to use List<Tuple<Vector2>> but it doesnt seem to work in the version, or so i think so how can i fix it?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Random = UnityEngine.Random;
    5.  
    6. public class DungeonSpawner : MonoBehaviour
    7. {
    8.     public GameObject normalChest, upgradeChest, eliteChest;
    9.  
    10.     private int nRooms = 10;
    11.     private int n = 50;
    12.     private float scale = 30f;
    13.  
    14.     public GameObject room, enemyRoom, corridor, empty, mark, floorNr, emptyRoom;
    15.  
    16.     private Vector2 spawnPos;
    17.  
    18.     public int[,] rooms;
    19.     public List<Tuple<Vector2, Vector2>> roomPositions;
    20.     private List<Tuple<Vector2, Vector2>> availableRooms;
    21.  
    22.     private List<Vector2> directionsList;
    23.     private List<Vector2> currentDirections;
    24.  
    25.     public List<GameObject> objects;
    26.    
    27.     public static DungeonSpawner Instance { get; set; }
    28.     private void Awake() {
    29.         Instance = this;
    30.         objects = new List<GameObject>();
    31.     }
    32.  
    33.     public void DeleteDungeon() {
    34.         print("Rooms :D " + objects.Count);
    35.         foreach (var g in objects) {
    36.             Destroy(g);
    37.         }
    38.     }
    39.  
    40.     public void GenerateNewDungeon() {
    41.         roomPositions = new List<Tuple<Vector2, Vector2>>();
    42.         rooms = new int[n,n];
    43.        
    44.         directionsList = new List<Vector2>();
    45.         directionsList.Add(new Vector2(0, 1));
    46.         directionsList.Add(new Vector2(0, -1));
    47.         directionsList.Add(new Vector2(1, 0));
    48.         directionsList.Add(new Vector2(-1, 0));
    49.  
    50.         Vector2 pos = new Vector2(25,25);
    51.         Vector2 size = new Vector2(Random.Range(1, 2), Random.Range(1, 2));
    52.         Tuple<Vector2, Vector2> firstRoom = new Tuple<Vector2, Vector2>(pos, size);
    53.         roomPositions.Add(firstRoom);
    54.         Instantiate(floorNr, pos * scale, Quaternion.identity);
    55.         markRoom(firstRoom, 3);
    56.        
    57.         for (int i = 0; i < nRooms; i++) {
    58.             int c = 0;
    59.             //Dont do this at home kids LOL
    60.             while (!GenerateNewRoom()) {
    61.                 c++;
    62.                 if (c > 100) break;
    63.             }
    64.         }
    65.         availableRooms = new List<Tuple<Vector2, Vector2>>(roomPositions);
    66.  
    67.         Vector2 finalRoom = availableRooms[availableRooms.Count - 1].Item1;
    68.         rooms[(int) finalRoom.x, (int) finalRoom.y] = 3;
    69.         FindSpawns();
    70.     }
    71.  
    72.     private void FindChests() {
    73.         int total = Random.Range(3, 5);
    74.         print("total: " + total);
    75.        
    76.         int normalChests = Random.Range(1, 4);
    77.         total -= normalChests;
    78.         int upgradeChests = total;
    79.         int eliteChests = 0;
    80.         if (Random.Range(0f, 1f) < 0.25f) eliteChests = 1;
    81.  
    82.         for (int i = 0; i < normalChests; i++) {
    83.             int r = Random.Range(0, availableRooms.Count);
    84.             Vector2 pos = availableRooms[r].Item1;
    85.             objects.Add(Instantiate(normalChest, pos * scale, Quaternion.identity));
    86.             availableRooms.RemoveAt(r);
    87.             rooms[(int) pos.x, (int) pos.y] = 3;
    88.         }
    89.        
    90.         for (int i = 0; i < upgradeChests; i++) {
    91.             int r = Random.Range(0, availableRooms.Count);
    92.             Vector2 pos = availableRooms[r].Item1;
    93.             objects.Add(Instantiate(upgradeChest, pos * scale, Quaternion.identity));
    94.             availableRooms.RemoveAt(r);
    95.             rooms[(int) pos.x, (int) pos.y] = 3;
    96.         }
    97.         for (int i = 0; i < eliteChests; i++) {
    98.             int r = Random.Range(0, availableRooms.Count);
    99.             Vector2 pos = availableRooms[r].Item1;
    100.             objects.Add(Instantiate(eliteChest, pos * scale, Quaternion.identity));
    101.             availableRooms.RemoveAt(r);
    102.             rooms[(int) pos.x, (int) pos.y] = 3;
    103.         }
    104.     }
    105.  
    106.     private void ShowAvailable() {
    107.         for (int i = 0; i < availableRooms.Count; i++) {
    108.             print(availableRooms[i].Item1.x + ", " + availableRooms[i].Item1.y );
    109.         }
    110.     }
    111.  
    112.     private void FindSpawns() {
    113.         Vector2 playerSpawn = roomPositions[0].Item1;
    114.         spawnPos = playerSpawn;
    115.         Vector2 bossSpawn = roomPositions[nRooms].Item1;
    116.  
    117.         availableRooms.RemoveAt(nRooms);
    118.         availableRooms.RemoveAt(0);
    119.         //Instantiate(mark, playerSpawn * scale, Quaternion.identity);
    120.         objects.Add(Instantiate(mark, bossSpawn * scale, Quaternion.identity));
    121.     }
    122.  
    123.     private void FindRandomLeaf() {
    124.        
    125.     }
    126.  
    127.     private bool GenerateNewRoom() {
    128.         int roomNumber = Random.Range(0, roomPositions.Count);
    129.         Vector2 npos = roomPositions[roomNumber].Item1;
    130.         Vector2 nsize = roomPositions[roomNumber].Item2;
    131.            
    132.         int x = Random.Range(0, (int) nsize.x);
    133.         int y = Random.Range(0, (int) nsize.y);
    134.            
    135.         //Find direction to create new room
    136.         currentDirections = new List<Vector2>();
    137.         currentDirections.Add(new Vector2(0, 1));
    138.         currentDirections.Add(new Vector2(0, -1));
    139.         currentDirections.Add(new Vector2(1, 0));
    140.         currentDirections.Add(new Vector2(-1, 0));
    141.         for (int u = 0; u < 4; u++) {
    142.             int nr = Random.Range(0, currentDirections.Count);
    143.             Vector2 dir = currentDirections[nr] * 2;
    144.             currentDirections.RemoveAt(nr);
    145.             if (!hasNeighbours(npos + dir)) {
    146.                 if (nNeighbours(npos+dir) > 2) continue;
    147.                 Vector2 p = npos + dir;
    148.                 Vector2 s = nsize;
    149.                 Tuple<Vector2, Vector2> nRoom = new Tuple<Vector2, Vector2>(p, s);
    150.                 roomPositions.Add(nRoom);
    151.                 markRoom(nRoom, 1);
    152.                 rooms[(int) (npos.x + (dir.x / 2)), (int) (npos.y + (dir.y / 2))] = 2;
    153.                 return true;
    154.             }
    155.         }
    156.         return false;
    157.     }
    158.  
    159.     private bool hasNeighbours(Vector2 pos) {
    160.         for (int y = -1; y < 2; y++) {
    161.             for (int x = -1; x < 2; x++) {
    162.                 Vector2 p = new Vector2(x, y) + pos;
    163.                 if (p.x < 0 || p.x > n || p.y < 0 || p.y > n) continue;
    164.                 if (rooms[(int) p.x, (int) p.y] != 0)
    165.                     return true;
    166.             }
    167.         }
    168.         return false;
    169.     }
    170.    
    171.     private int nNeighbours(Vector2 pos) {
    172.         int count = 0;
    173.  
    174.         if (rooms[(int) pos.x + 2, (int) pos.y] != 0) count++;
    175.         if (rooms[(int) pos.x - 2, (int) pos.y] != 0) count++;
    176.         if (rooms[(int) pos.x, (int) pos.y + 2] != 0) count++;
    177.         if (rooms[(int) pos.x, (int) pos.y - 2] != 0) count++;
    178.  
    179.         return count;
    180.     }
    181.  
    182.     private void markRoom(Tuple<Vector2, Vector2> newRoom, int mark) {
    183.         Vector2 pos = newRoom.Item1;
    184.         Vector2 size = newRoom.Item2;
    185.         for (int x = 0; x < size.x; x++) {
    186.             rooms[(int) pos.x + x, (int) pos.y] = mark;
    187.         }
    188.         for (int y = 1; y < size.y; y++) {
    189.             rooms[(int) pos.x, (int) pos.y + y] = mark;
    190.         }
    191.  
    192.         if (size.x == 2 && size.y == 2)
    193.             rooms[(int) pos.x + 1, (int) pos.y + 1] = mark;
    194.     }
    195.  
    196.     private void ShowRooms() {
    197.         for (int x = 0; x < n; x++) {
    198.             for (int y = 0; y < n; y++) {
    199.                 if (rooms[x, y] == 0) { //nothing
    200.                     GameObject r = Instantiate(empty, new Vector2(x * scale, y * scale), Quaternion.identity);
    201.                     r.transform.localScale = Vector2.one * scale;
    202.                     objects.Add(r);
    203.                 }
    204.  
    205.                 if (rooms[x, y] == 1) { //room
    206.                     GameObject r = Instantiate(enemyRoom, new Vector2(x * scale, y * scale), Quaternion.identity);
    207.                     r.transform.localScale = Vector2.one * scale;
    208.                     objects.Add(r);
    209.                 }
    210.                 else if (rooms[x, y] == 2) { //corridor
    211.                     float a = 0;
    212.                     if (rooms[x + 1, y] == 1 || rooms[x - 1, y] == 1) a = 90;
    213.                     if (rooms[x + 1, y] == 3 || rooms[x - 1, y] == 3) a = 90;
    214.                     Vector2 s = corridor.transform.localScale * scale;
    215.                     GameObject r = Instantiate(corridor, new Vector2(x * scale, y * scale), Quaternion.Euler(new Vector3(0,0,a)));
    216.                     r.transform.localScale = s;
    217.                     objects.Add(r);
    218.                 }
    219.                 else if (rooms[x, y] == 3) {
    220.                     GameObject r = Instantiate(emptyRoom, new Vector2(x * scale, y * scale), Quaternion.identity);
    221.                     r.transform.localScale = Vector2.one * scale;
    222.                     objects.Add(r);
    223.                 }
    224.             }
    225.         }
    226.     }
    227.  
    228.     public Vector2 GetSpawnPos() {
    229.         return spawnPos * scale;
    230.     }
    231. }
    232.  
     
  2. ShokTheIV

    ShokTheIV

    Joined:
    Nov 19, 2020
    Posts:
    17
    It says im missing a namespace but i dont really know wat name space it requires
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,893
    You need
    using System;


    Your IDE (Visual Studio or similar) should give you a suggestion for that.
     
  4. ShokTheIV

    ShokTheIV

    Joined:
    Nov 19, 2020
    Posts:
    17
    No im using a text editor bcs my frickin 2gb or ram of laptop cant run visual studio so im using visual sstudio code ):
     
  5. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,992
    Huh. VS Code worked better for me. It seemed to do everything Visual Studio did, without those big stalls when it thinks I want more hints, or reformatting my lines randomly.

    Instead of a tuple, a class is often more useful:
    Code (CSharp):
    1. class DungeonSquare_t {
    2.   public Vector2 p1, p2;
    3.   public int roomType;
    4.   ...
    5. }
    6.  
    7. List<DungeonSquare_t> DSquares;
    Tuples are for real quick stuff where making a class hardy seems worth it (say a function wants to return an int and a string, a tuple is great -- better than return-by-reference).
     
    Kurt-Dekker and PraetorBlue like this.