Browse Source

Added error logging for cases of components in detached nodes trying to find Octree or PhysicsWorld, or subscribing to update events.
Added error logging if a node is moved from one scene to another (unsupported.)

Lasse Öörni 12 years ago
parent
commit
b8270df90b

+ 1 - 1
Docs/Reference.dox

@@ -230,7 +230,7 @@ Whenever there is some hierarchical composition, it is recommended (and in fact
 
 %Scene nodes can be freely reparented. In contrast components are always created to the node they belong to, and can not be moved between nodes. Both child nodes and components are stored using SharedPtr containers; this means that detaching a child node from its parent or removing a component will also destroy it, if no other references to it exist. Both Node & Component provide the \ref Node::Remove "Remove()" function to accomplish this without having to go through the parent. Note that no operations on the node or component in question are safe after calling that function.
 
-It is also legal to create a Node that does not belong to a scene. This is particularly useful with cameras, because then the camera will not be serialized along with the actual scene, which is perhaps not always wanted.
+It is also legal to create a Node that does not belong to a scene. This is useful with cameras, because then the camera will not be serialized along with the actual scene, which is perhaps not always wanted. However, note that creating geometry, physics or script components to an unattached node, and then moving it into a scene later will cause those components to not work correctly.
 
 \section SceneModel_Update Scene updates and serialization
 

+ 8 - 0
Engine/Graphics/Drawable.cpp

@@ -25,6 +25,7 @@
 #include "Camera.h"
 #include "Context.h"
 #include "DebugRenderer.h"
+#include "Log.h"
 #include "Material.h"
 #include "Octree.h"
 #include "Scene.h"
@@ -382,6 +383,13 @@ void Drawable::AddToOctree()
         Octree* octree = scene->GetComponent<Octree>();
         if (octree)
             octree->InsertDrawable(this);
+        else
+            LOGERROR("No Octree component in scene, drawable will not render");
+    }
+    else
+    {
+        // We have a mechanism for adding detached nodes to an octree manually, so do not log this error
+        //LOGERROR("Node is detached from scene, drawable will not render");
     }
 }
 

+ 2 - 0
Engine/Physics/CollisionShape.cpp

@@ -747,6 +747,8 @@ void CollisionShape::OnNodeSet(Node* node)
             else
                 LOGERROR("No physics world component in scene, can not create collision shape");
         }
+        else
+            LOGERROR("Node is detached from scene, can not create collision shape");
         
         node->AddListener(this);
         cachedWorldScale_ = node->GetWorldScale();

+ 2 - 0
Engine/Physics/Constraint.cpp

@@ -374,6 +374,8 @@ void Constraint::OnNodeSet(Node* node)
             else
                 LOGERROR("No physics world component in scene, can not create constraint");
         }
+        else
+            LOGERROR("Node is detached from scene, can not create constraint");
         
         node->AddListener(this);
         cachedWorldScale_ = node->GetWorldScale();

+ 3 - 0
Engine/Physics/RigidBody.cpp

@@ -838,6 +838,9 @@ void RigidBody::OnNodeSet(Node* node)
 
             AddBodyToWorld();
         }
+        else
+            LOGERROR("Node is detached from scene, can not create rigid body");
+        
         node->AddListener(this);
     }
 }

+ 1 - 1
Engine/Scene/Node.cpp

@@ -529,7 +529,7 @@ void Node::AddChild(Node* node)
     node->Remove();
 
     // Add to the scene if not added yet
-    if (scene_ && !node->GetScene())
+    if (scene_ && node->GetScene() != scene_)
         scene_->NodeAdded(node);
 
     node->parent_ = this;

+ 10 - 1
Engine/Scene/Scene.cpp

@@ -647,9 +647,18 @@ unsigned Scene::GetFreeComponentID(CreateMode mode)
 
 void Scene::NodeAdded(Node* node)
 {
-    if (!node || node->GetScene())
+    if (!node || node->GetScene() == this)
         return;
 
+    // If node already exists in another scene, remove. This is unsupported, as components will not reinitialize themselves
+    // to use the new scene
+    Scene* oldScene = node->GetScene();
+    if (oldScene)
+    {
+        LOGERROR("Moving a node from one scene to another is unsupported");
+        oldScene->NodeRemoved(node);
+    }
+    
     node->SetScene(this);
 
     // If we already have an existing node with the same ID, must remove the scene reference from it

+ 9 - 0
Engine/Script/ScriptInstance.cpp

@@ -478,7 +478,10 @@ void ScriptInstance::UpdateEventSubscription()
 {
     Scene* scene = GetScene();
     if (!scene)
+    {
+        LOGERROR("Node is detached from scene, can not subscribe script object to update events");
         return;
+    }
     
     bool enabled = scriptObject_ && IsEnabledEffective();
     
@@ -503,6 +506,12 @@ void ScriptInstance::UpdateEventSubscription()
                 if (methods_[METHOD_FIXEDPOSTUPDATE])
                     SubscribeToEvent(world, E_PHYSICSPOSTSTEP, HANDLER(ScriptInstance, HandlePhysicsPostStep));
             }
+            else
+            {
+                if (methods_[METHOD_FIXEDUPDATE] || methods_[METHOD_FIXEDPOSTUPDATE])
+                    LOGERROR("No physics world, can not subscribe script object to fixed update events");
+            }
+            
             
             subscribedPostFixed_ = true;
         }