MCPcopy Create free account
hub / github.com/FlaxEngine/FlaxEngine / SetParent

Method SetParent

Source/Engine/Level/Actor.cpp:299–408  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

297}
298
299void 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 {

Callers 15

InvokeObjectSpawnFunction · 0.45
TestPrefabs.cppFile · 0.45
BuildDirtyBoundsFunction · 0.45
CreatePrefabMethod · 0.45
TickMethod · 0.45
GetOrAddChildFunction · 0.45
AddScriptFunction · 0.45
AddChildMethod · 0.45
GetOrAddChildMethod · 0.45
spawnActorMethod · 0.45
DeletePrefabObjectMethod · 0.45

Calls 10

IsActiveInHierarchyFunction · 0.85
IsInMainThreadFunction · 0.85
GetSceneFunction · 0.85
SetParentFunction · 0.85
LockMethod · 0.80
RemoveKeepOrderMethod · 0.80
UnlockMethod · 0.80
OnDoneMethod · 0.80
AddMethod · 0.45
WorldToLocalMethod · 0.45

Tested by

no test coverage detected