Browse Source

Ensure node's components are properly marked for network update in case node is late added to the scene. Make it impossible for a NetworkState to exist without properly allocated currentValues & previousValues. Fix erroneous comment in C++ version of SceneReplication sample. Code cleanup & minor optimization. Closes #1511.

Lasse Öörni 9 years ago
parent
commit
0991c8e078

+ 0 - 1
Source/Samples/17_SceneReplication/SceneReplication.cpp

@@ -99,7 +99,6 @@ void SceneReplication::CreateScene()
 {
     scene_ = new Scene(context_);
 
-    // Create scene content on the server only
     ResourceCache* cache = GetSubsystem<ResourceCache>();
 
     // Create octree and physics world with default settings. Create them as local so that they are not needlessly replicated

+ 0 - 10
Source/Urho3D/Scene/Component.cpp

@@ -166,16 +166,6 @@ void Component::PrepareNetworkUpdate()
 
     unsigned numAttributes = attributes->Size();
 
-    if (networkState_->currentValues_.Size() != numAttributes)
-    {
-        networkState_->currentValues_.Resize(numAttributes);
-        networkState_->previousValues_.Resize(numAttributes);
-
-        // Copy the default attribute values to the previous state as a starting point
-        for (unsigned i = 0; i < numAttributes; ++i)
-            networkState_->previousValues_[i] = attributes->At(i).defaultValue_;
-    }
-
     // Check for attribute changes
     for (unsigned i = 0; i < numAttributes; ++i)
     {

+ 3 - 10
Source/Urho3D/Scene/Node.cpp

@@ -832,6 +832,9 @@ void Node::AddChild(Node* node, unsigned index)
     node->parent_ = this;
     node->MarkDirty();
     node->MarkNetworkUpdate();
+    // If the child node has components, also mark network update on them to ensure they have a valid NetworkState
+    for (Vector<SharedPtr<Component> >::Iterator i = node->components_.Begin(); i != node->components_.End(); ++i)
+        (*i)->MarkNetworkUpdate();
 
     // Send change event
     if (scene_)
@@ -1649,16 +1652,6 @@ void Node::PrepareNetworkUpdate()
     const Vector<AttributeInfo>* attributes = networkState_->attributes_;
     unsigned numAttributes = attributes->Size();
 
-    if (networkState_->currentValues_.Size() != numAttributes)
-    {
-        networkState_->currentValues_.Resize(numAttributes);
-        networkState_->previousValues_.Resize(numAttributes);
-
-        // Copy the default attribute values to the previous state as a starting point
-        for (unsigned i = 0; i < numAttributes; ++i)
-            networkState_->previousValues_[i] = attributes->At(i).defaultValue_;
-    }
-
     // Check for attribute changes
     for (unsigned i = 0; i < numAttributes; ++i)
     {

+ 22 - 7
Source/Urho3D/Scene/Serializable.cpp

@@ -690,12 +690,12 @@ void Serializable::SetTemporary(bool enable)
 
 void Serializable::SetInterceptNetworkUpdate(const String& attributeName, bool enable)
 {
-    const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
+    AllocateNetworkState();
+
+    const Vector<AttributeInfo>* attributes = networkState_->attributes_;
     if (!attributes)
         return;
 
-    AllocateNetworkState();
-
     for (unsigned i = 0; i < attributes->Size(); ++i)
     {
         const AttributeInfo& attr = attributes->At(i);
@@ -712,11 +712,26 @@ void Serializable::SetInterceptNetworkUpdate(const String& attributeName, bool e
 
 void Serializable::AllocateNetworkState()
 {
-    if (!networkState_)
+    if (networkState_)
+        return;
+
+    const Vector<AttributeInfo>* networkAttributes = GetNetworkAttributes();
+    networkState_ = new NetworkState();
+    networkState_->attributes_ = networkAttributes;
+
+    if (!networkAttributes)
+        return;
+
+    unsigned numAttributes = networkAttributes->Size();
+
+    if (networkState_->currentValues_.Size() != numAttributes)
     {
-        const Vector<AttributeInfo>* networkAttributes = GetNetworkAttributes();
-        networkState_ = new NetworkState();
-        networkState_->attributes_ = networkAttributes;
+        networkState_->currentValues_.Resize(numAttributes);
+        networkState_->previousValues_.Resize(numAttributes);
+
+        // Copy the default attribute values to the previous state as a starting point
+        for (unsigned i = 0; i < numAttributes; ++i)
+            networkState_->previousValues_[i] = networkAttributes->At(i).defaultValue_;
     }
 }