| 2098 | } |
| 2099 | |
| 2100 | Actor* Actor::Clone() |
| 2101 | { |
| 2102 | // Collect actors to clone |
| 2103 | auto actors = ActorsCache::ActorsListCache.Get(); |
| 2104 | actors->Add(this); |
| 2105 | SceneQuery::GetAllActors(this, *actors); |
| 2106 | |
| 2107 | // Serialize objects |
| 2108 | MemoryWriteStream stream; |
| 2109 | if (ToBytes(*actors, stream)) |
| 2110 | return nullptr; |
| 2111 | |
| 2112 | // Remap object ids into a new ones |
| 2113 | auto modifier = Cache::ISerializeModifier.Get(); |
| 2114 | for (const Actor* actor : *actors.Value) |
| 2115 | { |
| 2116 | if (!actor) |
| 2117 | continue; |
| 2118 | modifier->IdsMapping.Add(actor->GetID(), Guid::New()); |
| 2119 | for (const Script* script : actor->Scripts) |
| 2120 | { |
| 2121 | if (script) |
| 2122 | modifier->IdsMapping.Add(script->GetID(), Guid::New()); |
| 2123 | } |
| 2124 | } |
| 2125 | if (HasPrefabLink() && HasParent() && !IsPrefabRoot()) |
| 2126 | { |
| 2127 | // When cloning actor that is part of prefab (but not as whole), ignore the prefab hierarchy |
| 2128 | Actor* parent = GetParent(); |
| 2129 | do |
| 2130 | { |
| 2131 | modifier->IdsMapping.Add(parent->GetPrefabObjectID(), Guid::Empty); |
| 2132 | parent = parent->GetParent(); |
| 2133 | } while (parent && !parent->IsPrefabRoot()); |
| 2134 | } |
| 2135 | |
| 2136 | // Deserialize objects |
| 2137 | Array<Actor*> output; |
| 2138 | if (FromBytes(ToSpan(stream), output, modifier.Value) || output.IsEmpty()) |
| 2139 | return nullptr; |
| 2140 | return output[0]; |
| 2141 | } |
| 2142 | |
| 2143 | void Actor::SetPhysicsScene(PhysicsScene* scene) |
| 2144 | { |