January 3, 2010

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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. ;)



2 Responses to “Correction: Component “Parent” Set Accessor”

  1.   Innovation Engine Roadmap 2009-2010 | Innovative Games Says:
      January 5, 2010 at 9:03 pm

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

  2.   mnn Says:
      March 7, 2010 at 2:14 am

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

comment Leave a Reply