1. We've introduced thread tags, search within a thread and similar thread search. Read more here.
    Dismiss Notice
  2. We've released our first Timeline Experimental Preview, our new tool for creating cutscenes and more! To check it out click here.
    Dismiss Notice
  3. All Unity Pro perpetual license customers: your special offer to subscribe is ready! Click here.
    Dismiss Notice
  4. Unity 5.5 is now released.
    Dismiss Notice
  5. Check out all the fixes for 5.5 in patch releases 1 & 2.
    Dismiss Notice
  6. Unity 5.6 beta is now available for download.
    Dismiss Notice
  7. Get prepared for the Tizen Mobile App Incentive Program! Read more about the upcoming program here.
    Dismiss Notice
  8. Enter the Microsoft Developer Challenge for a chance to win prizes. Read more about it here.
    Dismiss Notice

What is the best way to structure this class

Discussion in 'Scripting' started by aubergine, Feb 26, 2011.

  1. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Messages:
    2,348
    Probably a very simple question but i lost it at this point.

    I have a class like follows:

    public class Person {
    public string name;
    public List<Person> children = new List<Person>();
    }

    when i instance this class, it creates 1 person and few children then few more children for the previous children then..etc.

    I dont want that, i want children list to be instanced once. So do i have to seperate children list or is there any other way?
     
  2. Cameron860

    Cameron860

    Joined:
    Jun 1, 2009
    Messages:
    763
    public static List<Person> children = new List<Person>();

    Static means the field belongs to the Class, not every instance of that class.

    You can then access Person.children[...] from anywhere in any other script or just children[...] from within an instance of a Person class and they will all point to a single List.
     
  3. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Messages:
    2,348
    I meant i want a children list for every person but i dont want a children list for every persons children.
    If i make it a static then wouldnt it mean theres 1 child list at all?

    Anyways i made a childperson class to wrap things up. and it does the job.
     
  4. Antitheory

    Antitheory

    Joined:
    Nov 14, 2010
    Messages:
    549
    I would make a class Person that contains everything common to your Persons. Then I would make classes "Parent" and "Child" derived from Person.
     
  5. Cameron860

    Cameron860

    Joined:
    Jun 1, 2009
    Messages:
    763
    Arr I see.

    Technically, this shouldn't be an issue as long as your constructor/awake/start code does not create more person instances.

    Generally with this kind of a structure you build/populate it from an external class/script so you don't have people creating other people all willy nilly (like the 70s).

    Having empty List<Person>'s won't really cause any issues. The other options is to create a "soft collection" where you have public pointers to other Persons:

    public Person parent = null;
    public Person firstChild = null;
    public Person nextChild = null;

    As long as your code reinforces these references then all should be fine. public properties with custom get/set code works wonders here, you can also implement array access [...] on top of all this, but that's kinda overkill IMO, may as well just use a list.
     
    Last edited: Feb 26, 2011