Jump to content
  • Advertisement
Sign in to follow this  
Alundra

Safe problem in scene Manager about root array + linear array

This topic is 1018 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,

I have a root actor array which is used to update recursively and load/save, the linear array is used to find actor and for the rendering.

To manage these two arrays the scene manager has two functions :

void AddActorInRoot( IActor* Actor );
void RemoveActorFromRoot( IActor* Actor );

These two functions only add in the root array or remove from the root array.

It's called by SetParent of the actor, if nullptr it's added in root array otherwise removed from root array.

The problem is since these two function are public, it's not safe, user of the API can call these functions everywhere.

Any idea to avoid this safe problem ?

Thanks

Edited by Alundra

Share this post


Link to post
Share on other sites
Advertisement

You could solve it using a change-if-needed paradigm.

 

SceneManager::AddActorInRoot(IActor &actor) checks if the actor is already a member of the root list. If it is not, then it adds it to the list and calls IActor::SetParent(this).

 

SceneManager::RemoveActorFromRoot(IActor &actor) checks if the actor is a member of the root list. If it is not, then we do nothing. Otherwise, we remove it from the list and call IActor::SetParent(null).

 

Actor::SetParent(SceneManager* new_parent) first checks if the new parent is the same as its current parent. If it is, we return without doing anything. If its current_parent is not null, we call current_parent->RemoveActorFromRoot(*this), then sets current_parent to the new_parent. Then if the new parent is not null, call new_parent->AddActorToRoot(*this).

 

 

The loop is not infinite because we recurse only if the requested final state is not the current state. We can call from either end (SetParent or AddActorInRoot/RemoveActorFromRoot) first because either path ends up calling the other if necessary. And as a Java programmer looking back on my C++ days, I strongly suggest using a reference parameter instead of a pointer wherever taking a null pointer as an argument would be 100% unacceptable.

Edited by Wyrframe

Share this post


Link to post
Share on other sites

I posted there because scene manager is part of the rendering code, the category is called : "Graphics Programming and Theory".

About the problem, other solution is to have one array and always have it sorted parent -> child but I don't know performance of that.

Share this post


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

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!