Search Unity

Help with designing data structure?

Discussion in 'Scripting' started by weitay, Dec 21, 2014.

  1. weitay

    weitay

    Joined:
    Aug 5, 2012
    Posts:
    10
    Hi,

    I'm not sure if I am using the right terminology but here goes...

    I am trying to basically make a 2d room builder that takes input from the variables exposed to the editor. So I have a 2d List which looks like this.

    Code (CSharp):
    1. [System.Serializable]
    2. public class RoomInfo
    3. {
    4.      public bool hasChair;
    5.      public bool hasTable;
    6. }
    7. [System.Serializable]
    8. public class RoomsLayout
    9. {
    10.      public List<RoomInfo> Rooms;
    11. }
    12. [System.Serializable]
    13. public class InteriorLayout
    14. {
    15.      public List<RoomsLayout> Floors;
    16. }
    17.  
    18. public class RoomBuilder: MonoBehaviour
    19. {
    20.       public InteriorLayout Layout;
    21.  
    22. ...
    23. }
    24.  
    25.  
    Now I cant quite figure out what is a good way to extend this. So say I wanted to extend this room builder to be a fancy room builder, so I go ahead and extend the RoomInfo class like this

    Code (CSharp):
    1. [System.Serializable]
    2. public class FancyRoomInfo : RoomInfo
    3. {
    4.      public bool hasPainting;
    5. }
    but in my new fancy room builder I have the old list from the parent which I cannot override or reassign!
     
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    I think you might be inheriting from the wrong class. RoomInfo does not seem to contain any lists just two boolean variables. What you might want to do is either rethink your logic or inherent from RoomsLayout and for every RoomsLayout have a boolean to see if the type is fancy.

    I suggest to have an enum that is a member of RoomInfo and mark them to default as DEFAULT = 0 and have an enum value called FANCY etc. Then when you loop through the builder you can check if the room is a DEFAULT room or FANCY room and do create routines for each type of room.

    Hope this helps.
     
  3. MathiasDG

    MathiasDG

    Joined:
    Jul 1, 2014
    Posts:
    114
  4. weitay

    weitay

    Joined:
    Aug 5, 2012
    Posts:
    10
    Hmm ok, but that works only because the building is made up of only normal rooms or fancy rooms.

    What about if the builder is extended to make a hospital and is made up of normal rooms and an operation room instead. The enum would contain fancy room which shouldnt be exposed to a hospital builder.