Correction: Component “Parent” Set Accessor

The “Set” accessor in the Component’s “Parent” property should be changed to the following to avoid a condition where a component’s would be “orphaned” (have its “parent” property set to null and not appear in any GameScreen’s list of components) when its parent is set to the same value.

public GameScreen Parent
{
    get { return parent; }
    set
    {
        if (parent == value)
            return;

        if (parent != null)
            parent.RemoveComponent(this);

        parent = value;

        if (value != null)
            parent.AddComponent(this);
    }
}

Thanks go to “Shane” for finding this bug. And for making me actually run all the stack traces to check my code instead of lazily assuming it would work. ;)

7 Responses to “Correction: Component “Parent” Set Accessor” »

  1. Pingback by Innovation Engine Roadmap 2009-2010 | Innovative Games — January 5, 2010 @ 9:03 pm

    [...] Correction: Component “Parent” Set Accessor [...]

  2. Comment by mnn — March 7, 2010 @ 2:14 am

    Why I have such feeling that this the Parent property is the same as in the GameScreen article :D

  3. Comment by tj — April 16, 2010 @ 3:12 pm

    so do i worry about this after building the gamescreen?

  4. Comment by TimothyStewart — April 15, 2011 @ 4:49 am

    =

  5. Comment by Espilon — May 12, 2011 @ 4:37 am

    If this is only set via the GameScreen.AddComponent() method then the last two lines are not needed since all they do is create a loop, I haven’t seen this property set from anywhere else so I think this is the case, the loop is stopped by the AddComponent() method checking if the component is already in it’s list however so it’s not that bad I suppose

  6. Comment by Evannymaind — July 14, 2011 @ 8:59 am

    =

  7. Comment by badoquibiadut — September 13, 2011 @ 11:18 am

    =

RSS feed for comments on this post. TrackBack URI

Leave a comment