| 297 | } |
| 298 | |
| 299 | void Actor::SetParent(Actor* value, bool worldPositionsStays, bool canBreakPrefabLink) |
| 300 | { |
| 301 | if (_parent == value) |
| 302 | return; |
| 303 | #if USE_EDITOR || !BUILD_RELEASE |
| 304 | if (Is<Scene>()) |
| 305 | { |
| 306 | LOG(Error, "Cannot change parent of the Scene. Use Level to manage scenes."); |
| 307 | return; |
| 308 | } |
| 309 | #endif |
| 310 | |
| 311 | // Peek the previous state |
| 312 | const Transform prevTransform = _transform; |
| 313 | const bool wasActiveInTree = IsActiveInHierarchy(); |
| 314 | const auto prevParent = _parent; |
| 315 | const auto prevScene = _scene; |
| 316 | const auto newScene = value ? value->_scene : nullptr; |
| 317 | |
| 318 | // Detect it actor is not in a game but new parent is already in a game (we should spawn it) |
| 319 | const bool isBeingSpawned = !IsDuringPlay() && newScene && value->IsDuringPlay(); |
| 320 | |
| 321 | // Actors system doesn't support editing scene hierarchy from multiple threads |
| 322 | if (!IsInMainThread() && (IsDuringPlay() || isBeingSpawned)) |
| 323 | { |
| 324 | LOG(Error, "Editing scene hierarchy is only allowed on a main thread."); |
| 325 | return; |
| 326 | } |
| 327 | |
| 328 | // Handle changing scene (unregister from it) |
| 329 | const bool isSceneChanging = prevScene != newScene; |
| 330 | if (prevScene && isSceneChanging && wasActiveInTree) |
| 331 | { |
| 332 | OnDisableInHierarchy(); |
| 333 | } |
| 334 | |
| 335 | Level::ScenesLock.Lock(); |
| 336 | |
| 337 | // Unlink from the old one |
| 338 | if (_parent) |
| 339 | { |
| 340 | _parent->Children.RemoveKeepOrder(this); |
| 341 | _parent->_isHierarchyDirty = true; |
| 342 | } |
| 343 | |
| 344 | // Set value |
| 345 | _parent = value; |
| 346 | |
| 347 | // Link to the new one |
| 348 | if (_parent) |
| 349 | { |
| 350 | _parent->Children.Add(this); |
| 351 | _parent->_isHierarchyDirty = true; |
| 352 | } |
| 353 | |
| 354 | // Sync scene change if need to |
| 355 | if (isSceneChanging) |
| 356 | { |
no test coverage detected