Search Unity

Which structure to use

Discussion in 'Scripting' started by oliver_unity892, Mar 11, 2021.

  1. oliver_unity892

    oliver_unity892

    Joined:
    Oct 28, 2019
    Posts:
    54
    Hi all

    I want to keep a list of objects that relate to other objects. For example, furniture, where a kitchen table might relate to a tablecloth, but a kitchen table might also relate to chairs. Chairs might also relate to a desk, as well as to a garden table.

    Each object name might have multiple entries:
    table <-> Chair
    table <-> Table Cloth
    table <-> coaster
    coaster <-> coffee table
    etc
    I can't use a dictionary as you can't have keys with the same value in there. What other efficient ways are there of storing that type of structure such that searching isn't terrible?

    I thought about using a class called Relationship with two public variables (perhaps Key and Value), and then creating a list of Relationships.

    Any other ways I've missed?

    Olly
     
  2. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    215
    How about a simple tree-like structure. Allow each
    class Furniture
    to have its own
    List<Furniture>
    .
     
  3. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,564
    A common trick is to start with something simple. like a list, but accessed through functions, and then change it later as you see more of what you need. If you never have more than 10 or 20 things you relate to, a list is fine (maybe keep it sorted?). If you often go through everything you relate to, a list is best, and so on.,

    By functions, I means
    bool relatesTo(item N) { return myRelations.contains(N); }
    . If you decide you need a Dictionary, the inside changes to myRelations[N]!=null, but the users keep using table.relatesTo(chair), same as always. That's one of the reasons OOP programming was invented -- you can just make it work, then easily adjust the details later.
     
unityunity