| 1430 | } |
| 1431 | |
| 1432 | void NetworkReplicator::AddObject(ScriptingObject* obj, const ScriptingObject* parent) |
| 1433 | { |
| 1434 | if (!obj || NetworkManager::IsOffline()) |
| 1435 | return; |
| 1436 | PROFILE_MEM(Networking); |
| 1437 | ScopeLock lock(ObjectsLock); |
| 1438 | if (Objects.Contains(obj)) |
| 1439 | return; |
| 1440 | |
| 1441 | // Automatic parenting for scene objects |
| 1442 | if (!parent) |
| 1443 | { |
| 1444 | auto sceneObject = ScriptingObject::Cast<SceneObject>(obj); |
| 1445 | if (sceneObject) |
| 1446 | parent = sceneObject->GetParent(); |
| 1447 | } |
| 1448 | |
| 1449 | // Ensure to register object in a scripting system (eg. lookup by ObjectId will work) |
| 1450 | if (!obj->IsRegistered()) |
| 1451 | obj->RegisterObject(); |
| 1452 | |
| 1453 | // Add object to the list |
| 1454 | NetworkReplicatedObject item; |
| 1455 | item.Object = obj; |
| 1456 | item.AsNetworkObject = ScriptingObject::ToInterface<INetworkObject>(obj); |
| 1457 | item.ObjectId = obj->GetID(); |
| 1458 | item.ParentId = parent ? parent->GetID() : Guid::Empty; |
| 1459 | item.OwnerClientId = NetworkManager::ServerClientId; // Server owns objects by default |
| 1460 | item.Role = NetworkManager::IsClient() ? NetworkObjectRole::Replicated : NetworkObjectRole::OwnedAuthoritative; |
| 1461 | NETWORK_REPLICATOR_LOG(Info, "[NetworkReplicator] Add new object {}:{}, parent {}:{}", item.ToString(), obj->GetType().ToString(), item.ParentId.ToString(), parent ? parent->GetType().ToString() : String::Empty); |
| 1462 | for (const SpawnItem& spawnItem : SpawnQueue) |
| 1463 | { |
| 1464 | if (spawnItem.HasOwnership && spawnItem.HierarchicalOwnership) |
| 1465 | { |
| 1466 | if (IsParentOf(obj, spawnItem.Object)) |
| 1467 | { |
| 1468 | // Inherit ownership |
| 1469 | item.Role = spawnItem.Role; |
| 1470 | item.OwnerClientId = spawnItem.OwnerClientId; |
| 1471 | break; |
| 1472 | } |
| 1473 | } |
| 1474 | } |
| 1475 | Objects.Add(MoveTemp(item)); |
| 1476 | if (Hierarchy && item.Role == NetworkObjectRole::OwnedAuthoritative) |
| 1477 | Hierarchy->AddObject(obj); |
| 1478 | } |
| 1479 | |
| 1480 | void NetworkReplicator::RemoveObject(ScriptingObject* obj) |
| 1481 | { |
no test coverage detected