Jump to content
  • Advertisement
Sign in to follow this  
Alundra

SetOwner + Remove Best Practice

This topic is 1665 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all,

I have a scene manager who has array of actor who has array of actor component, modern design.

ActorComponent has SetOwner/GetOwner functions, Actor has AddComponen/RemoveComponent.

Here the actual code of the SetOwner, AddComponent and RemoveComponent :

void IActorComponent::SetOwner( IActor* Actor )
{
  // Check if the owner is the same.
  if( m_Owner == Actor )
    return;

  // Remove the affector from the owner.
  if( m_Owner != NULL )
  {
    for( UInt32 i = 0; i < m_Owner->GetComponentCount(); ++i )
    {
      if( m_Owner->GetComponent( i ) == this )
      {
        m_Owner->RemoveComponent( i );
        break;
      }
    }
  }

  // Set the new owner.
  m_Owner = Actor;
}

void IActor::AddComponent( IActorComponent* Component )
{
  Component->SetOwner( this );
  m_ActorComponentArray.Add( Component );
}

void IActor::RemoveComponent( const UInt32 Index )
{
  m_ActorComponentArray[ Index ]->SetOwner( NULL );
}

That works but SetOwner does a for-loop for nothing since the index is already knew.

The best practice is to have a boolean on SetOwner to avoid the for-loop and Remove using index in RemoveComponent ?

if( RemoveFromOwner && (m_Owner != NULL) )

Another solution is to not manage remove/add in SetOwner and have :

void IActor::RemoveComponent( const UInt32 Index )
{
  m_ActorComponentArray[ Index ]->SetOwner( NULL );
  m_ActorComponentArray.Remove( Index );
}

Thanks for your opinion

 

---

EDIT : I just realized that AddComponent should just be :

Component->SetOwner( this );

Because SetOwner should add the component to the new owner.

Edited by Alundra

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!