/// <summary> /// Try lock entity and announce to other players (runs only on host) /// </summary> bool TryLockEntity(uint entityId, byte playerId, bool enable) { bool success = true; if (enable) { using (m_lockedEntitiesLock.AcquireExclusiveUsing()) { success = !m_lockedEntities.ContainsKey(entityId); if (success) { m_lockedEntities[entityId] = playerId; MyEntities.GetEntityById(entityId.ToEntityId()).OnClosing += m_unlockOnClosing; } } } else { using (m_lockedEntitiesLock.AcquireExclusiveUsing()) { success = m_lockedEntities.Remove(entityId); MyEntity entity; if (MyEntities.TryGetEntityById(entityId.ToEntityId(), out entity)) { entity.OnClosing -= m_unlockOnClosing; } } } if (success) { // Send lock to all MyEventLock response = new MyEventLock(); response.EntityId = entityId; response.LockType = enable ? MyLockEnum.LOCK : MyLockEnum.UNLOCK; Peers.SendToAll(ref response, NetDeliveryMethod.ReliableOrdered, 0); } if (enable) { // Send response to player MyPlayerRemote player; if (Peers.TryGetPlayer(playerId, out player)) { MyEventLockResult response = new MyEventLockResult(); response.EntityId = entityId; response.IsSuccess = success; Peers.NetworkClient.Send(ref response, player.Connection, NetDeliveryMethod.ReliableOrdered, 0); } } return(success); }
private void OnLockResult(ref MyEventLockResult msg) { if (msg.IsSuccess) { m_lockedEntities[msg.EntityId] = MyEntityIdentifier.CurrentPlayerId; } MyEntity entity; if (MyEntities.TryGetEntityById(new MyEntityIdentifier(msg.EntityId), out entity)) { RaiseLockResponse(entity, msg.IsSuccess); } }