Search Unity

void/class question

Discussion in 'Scripting' started by Loff, Feb 16, 2018.

  1. Loff

    Loff

    Joined:
    Dec 3, 2012
    Posts:
    81
    Hello,

    Example:
    Code (CSharp):
    1.   public Instance(string Name, Room room)
    2.     {
    3.         name = Name;
    4.         rooms.Add(room);
    5.     }
    6.  
    7.     public Instance(string Name, Room room1, Room room2, Room room3)
    8.     {
    9.         name = Name;
    10.         rooms.Add(room1);
    11.         rooms.Add(room2);
    12.         rooms.Add(room3);
    13.     }
    Lets say I wanted to add multiple rooms, but I don't want to add "Room room1, Room room2, Room room3" as it will be quite messy if you wanted to ask for 20 rooms or whatever.
    How would you handle this?
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Send in a list or array?
     
  3. Loff

    Loff

    Joined:
    Dec 3, 2012
    Posts:
    81
    Ended up using the examples below - feel free to comment if you know a better way.
    Code (CSharp):
    1.     public Instance(string Name, List <Room> Rooms)
    2.     {
    3.         name = Name;
    4.         rooms = Rooms;
    5.     }
    Code (CSharp):
    1.     void CreateInstances()
    2.     {
    3.  
    4.         instances.Add(new Instance("Building",
    5.                 new List<Room>
    6.                 {
    7.                    new Room(
    8.                        new List<Character>
    9.                         {
    10.                            CD.room[1],
    11.                            CD.room[1]
    12.                         }
    13.                    ),
    14.                     new Room(
    15.                        new List<Character>
    16.                         {
    17.                            CD.room[1],
    18.                            CD.room[2]
    19.                         }
    20.                    )
    21.                 }
    22.             )
    23.         );
    24.  
    25.     }
    Don't worry about the actual names of the vars :)
     
    Last edited: Feb 16, 2018
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I don't really understand your question.
     
  5. Loff

    Loff

    Joined:
    Dec 3, 2012
    Posts:
    81
    Forgot to remove the question after I found a solution for it.. sorry. :)
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ah okay, that makes more sense. :) Glad you got it worked out.