Skip to main content
gamedev.net
🔒 Locked

Scene design

Started by domin 1 reply 650+ views
Original Post
domin
domin
Hi all - at first sorry for my poor english... Lets assume that we have two basic scene graph classes CNode and CScene. Due to design issues they must know following:

class CScene
{
	protected
		// CScene must know all of its children:
		CList<CNode*>	_Children;
	public
		// It alsmo must be able to add new children:
		void AddNode(CNode* Node)
		{
			Node->SetScene(this);
		
			_Children.Add(Node);
		}
}

class CNode
{
	protected
		// CNode must know its parent scene:
		CScene*			_Scene;
	public
		// PROBLEM
		// This method must be public, because it is called in
		// CScene::AddNode()
		// But end user SHOULD NOT HAVE ACCES TO IT, because it is
		// only usable in CScene::AddNode(), where _Children.Add()
		// is also called (without this it has only 'half' of functionality)
		void SetScene(CScene* Scene)
		{
			_Scene = Scene;
  		}
  		
  		// So, SOLUTION 1:
  		// Leave this method public and make it fully functional:
  		// PROBLEM:
  		// Of course, infinite recursion
		void SetScene(CScene* Scene)
		{
			_Scene = Scene;
			
			Scene->AddNode(this);
  		}
  		
  		// So, SOLUTION 2:
  		// Make this method fully functional and add additional bool parameter,
  		// inside CScene::AddNode() set it to false, outside to true (default)
  		// PROBLEM:
  		// End user have an additional, confusing parameter
		void SetScene(CScene* Scene, bool Param = true)
		{
			_Scene = Scene;
			
			if (Param)
			{
				Scene->AddNode(this);
   			}
  		}
}

Is there any clear way to implement that situation?
Programmer16
Programmer16
If I read your post correctly, you want CScene to have access to CNode::SetScene? If so, you can just use the friend keyword:
class CScene{	protected:		// CScene must know all of its children:		CList<CNode*>	_Children;	public:		// It alsmo must be able to add new children:		void AddNode(CNode* Node)		{			Node->SetScene(this);					_Children.Add(Node);		}}class CNode{	protected:		// CScene can access any CNode member.		friend CScene;		CScene*			_Scene;		void SetScene(CScene* Scene)		{			_Scene = Scene;  		}}


Hopefully that helps
domin
domin
Good point, thanks for answer.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.