Browse Source

Add note to discourage use of AddComponent(). Prevent attempting to assign model resource on StaticModel / AnimatedModel when no node set. Closes #1510.

Lasse Öörni 9 years ago
parent
commit
2f25c3b98d

+ 6 - 0
Source/Urho3D/Graphics/AnimatedModel.cpp

@@ -322,6 +322,12 @@ void AnimatedModel::SetModel(Model* model, bool createBones)
     if (model == model_)
     if (model == model_)
         return;
         return;
 
 
+    if (!node_)
+    {
+        URHO3D_LOGERROR("Can not set model while model component is not attached to a scene node");
+        return;
+    }
+
     // Unsubscribe from the reload event of previous model (if any), then subscribe to the new
     // Unsubscribe from the reload event of previous model (if any), then subscribe to the new
     if (model_)
     if (model_)
         UnsubscribeFromEvent(model_, E_RELOADFINISHED);
         UnsubscribeFromEvent(model_, E_RELOADFINISHED);

+ 6 - 0
Source/Urho3D/Graphics/StaticModel.cpp

@@ -243,6 +243,12 @@ void StaticModel::SetModel(Model* model)
         return;
         return;
     }
     }
 
 
+    if (!node_)
+    {
+        URHO3D_LOGERROR("Can not set model while model component is not attached to a scene node");
+        return;
+    }
+
     // Unsubscribe from the reload event of previous model (if any), then subscribe to the new
     // Unsubscribe from the reload event of previous model (if any), then subscribe to the new
     if (model_)
     if (model_)
         UnsubscribeFromEvent(model_, E_RELOADFINISHED);
         UnsubscribeFromEvent(model_, E_RELOADFINISHED);

+ 1 - 1
Source/Urho3D/Scene/Node.h

@@ -574,7 +574,7 @@ public:
     void MarkReplicationDirty();
     void MarkReplicationDirty();
     /// Create a child node with specific ID.
     /// Create a child node with specific ID.
     Node* CreateChild(unsigned id, CreateMode mode);
     Node* CreateChild(unsigned id, CreateMode mode);
-    /// Add a pre-created component.
+    /// Add a pre-created component. Using this function from application code is discouraged, as component operation without an owner node may not be well-defined in all cases. Prefer CreateComponent() instead.
     void AddComponent(Component* component, unsigned id, CreateMode mode);
     void AddComponent(Component* component, unsigned id, CreateMode mode);
     /// Calculate number of non-temporary child nodes.
     /// Calculate number of non-temporary child nodes.
     unsigned GetNumPersistentChildren() const;
     unsigned GetNumPersistentChildren() const;