public GameStateManager()
 {
     _saveables       = new List <ISaveable>();
     GameState        = new GameState();
     DefaultContainer = new SaveableContainerJObject("DefaultContainer");
     RegisterSaveable(DefaultContainer);
     IsApplicationQuitting = false;
     _isSynchronizingState = false;
     Application.quitting += () => IsApplicationQuitting = true;
 }
Esempio n. 2
0
        private void Awake()
        {
            _container = new SaveableContainerJObject("Null");

            foreach (var cachedSaveableComponent in serializedSaveableComponents)
            {
                _saveableComponents.Add(cachedSaveableComponent.identifier,
                                        (ISaveableComponent)cachedSaveableComponent.component);
            }

            if (!manualSaveLoad)
            {
                SaveSystemSingleton.Instance.GameStateManager.RegisterSaveable(this);
            }
        }
Esempio n. 3
0
        /// <inheritdoc/>
        public void Load(JObject state)
        {
            if (loadOnce && _hasLoaded)
            {
                return;
            }

            // Ensure it loads only once with the loadOnce parameter set to true
            _hasLoaded = true;

            if (_container == null)
            {
                _container = new SaveableContainerJObject("Saveable GameObject Container");
            }
            _container.Load(state);

            foreach (var pair in _saveableComponents)
            {
                string             saveableComponentId = pair.Key;
                ISaveableComponent saveableComponent   = pair.Value;

                if (saveableComponent == null)
                {
                    Debug.LogError(
                        $"Failed to load component with id: {saveableComponentId}. Component is potentially destroyed.");
                }
                else
                {
                    if (_container.TryGetValue(saveableComponentId, out var data, saveableComponent.SaveDataType))
                    {
                        saveableComponent.Load(data);
                    }
                }
            }

            // clear empty records if any
            foreach (var emptyKey in _saveableComponents.Where(pair => pair.Value == null).Select(pair => pair.Key)
                     .ToArray())
            {
                _saveableComponents.Remove(emptyKey);
            }
        }