Safe problem in scene Manager about root array + linear array

Started by
2 comments, last by Alundra 8 years, 6 months ago

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

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.

RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
This should be moved to Game Programming or General Programming.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

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.

This topic is closed to new replies.

Advertisement