Adding a component that already exists

80 views
Skip to first unread message

John Nyquist

unread,
Jan 21, 2016, 10:45:08 PM1/21/16
to Ash Framework
This was a valuable lesson for me tonight:
If you add a component to an entity and the entity already has it the entity first removes it. Removing it causes a remove signal to be dispatched. Then the entity adds it, causing an add signal to be dispatched. That is not a bug, but my code that dealt with the adding and removing resulted in some puzzling behavior that took my an hour or so to figure out. Here's the code where it happens. 

public function add( component : Object, componentClass : Class = null ) : Entity
{
if ( !componentClass )
{
componentClass = Class( component.constructor );
}
if ( components[ componentClass ] )
{
remove( componentClass );
}
components[ componentClass ] = component;
componentAdded.dispatch( this, componentClass );
return this;
}

public function remove( componentClass : Class ) : *
{
var component : * = components[ componentClass ];
if ( component )
{
delete components[ componentClass ];
componentRemoved.dispatch( this, componentClass );
return component;
}
return null;
}
Reply all
Reply to author
Forward
0 new messages