In the following scripts, how can I store the puzzle advancement of the player as datas?…
- User
In the following scripts, how can I store the puzzle advancement of the player as datas? So the important variables related to the puzzle can be saved and load.
using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; using UnityEngine;
[System.Serializable] public class GameData { public int character; public float health; public float stamina; public float flashlightAmount; public int ammo; public float fearLevel; public float sanityLevel; public bool insaneMode; public int trainDestroyed; public int deathCount; public int headShot; public int fatalHeadShot; public int numberOfItems; public int numberOfContainerItems; public Vector3 playerPosition; public bool quest1; public bool quest2; public bool quest3; public SerializableDictionary<string, bool> enemiesKilled; public SerializableDictionary<string, bool> itemsCollected; public SerializableDictionary<string, bool> narrativeDestroyed; public InventoryManager.Currency currency; public List items = new List(); public List containers = new List(); public List containers1 = new List(); public List containers2 = new List(); public List containers3 = new List(); public List containers4 = new List(); public List containers5 = new List();
public List<LovecraftianPuzzle.EldritchSymbols> currentOrder; public List<LovecraftianPuzzle.Glyphs> glyphs; //Options preferences //Doors //Mini events public GameData() { character = menuScript1.PlayerSelected; if (character == 1) { playerPosition = new Vector3(268.92f, 52.337f, 219.93f); } else if (character == 2) { playerPosition = new Vector3(-44.85f, 71.27088f, 108.83f); } else if (character == 3) { playerPosition = new Vector3(-44.85f, 71.27088f, 108.83f); } this.health = 100; this.stamina = 100; this.flashlightAmount = 100; this.ammo = 0; this.fearLevel = 0; this.sanityLevel = 30; this.insaneMode = false; this.trainDestroyed = 0; this.deathCount = 0; this.headShot = 0; this.fatalHeadShot = 0; this.numberOfItems = 0; this.numberOfContainerItems = 0; enemiesKilled = new SerializableDictionary<string, bool>(); itemsCollected = new SerializableDictionary<string, bool>(); narrativeDestroyed = new SerializableDictionary<string, bool>(); currency= new InventoryManager.Currency(); currency.Name = "Gold"; items = new List<PlayerItemData>(); containers= new List<ContainerItemData>(); containers1 = new List<ContainerItemData>(); containers2 = new List<ContainerItemData>(); containers3 = new List<ContainerItemData>(); containers4 = new List<ContainerItemData>(); containers5 = new List<ContainerItemData>(); this.quest1 = false; this.quest2 = false; this.quest3 = false; currentOrder = new List<LovecraftianPuzzle.EldritchSymbols>(); }}
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class LovecraftianPuzzle : MonoBehaviour, IDataPersistence { public enum EldritchSymbols { Nyarlathotep, Azathoth, Cthulhu, ShubNiggurath, YogSothoth } public Transform[] symbolSlots; public GameObject[] eldritchSymbolPrefabs; public GameObject[] glyphsGameObject; public List glyphs; public List books; public GameObject portal; public Animator portalAnimator; public AudioSource gatewayAudio; public AudioClip placeSymbolSound; public AudioClip puzzleSolvedSound; public GameObject bookParticlePrefab; public GameObject shadowPeoplePrefab; private GameObject player; private AudioSource playerAudioSource; private InventoryManager InvManager; private AudioSource puzzleAudio; private FearSystem fearSystem; private QuestSystem questSystem;
- Assistant
- User
I need to save and load the glyphs’ display using the LoadData(GameData data) and SaveData(GameData data) functions. I already handle the writing method in another script.
- Assistant
- User
ArgumentException: An item with the same key has already been added. Key: Nyarlathotep System.Collections.Generic.Dictionary
2[TKey,TValue].TryInsert (TKey key, TValue value, System.Collections.Generic.InsertionBehavior behavior) (at <17d9ce77f27a4bd2afb5ba32c9bea976>:0) System.Collections.Generic.Dictionary2[TKey,TValue].Add (TKey key, TValue value) (at <17d9ce77f27a4bd2afb5ba32c9bea976>:0) LovecraftianPuzzle.SaveData (GameData data) (at Assets/MyPuzzle/LovecraftianPuzzle.cs:333) - Assistant
The error
ArgumentException: An item with the same key has already been addedtypically occurs when you try to add a key-value pair to aDictionarywhere the key already exists. This means you're trying to add a duplicate key to theglyphsStatedictionary in yourSaveDatamethod.There are a few things to check to resolve this issue:
- User
Okay, now that the glyphs dictionnary is saving correctly, how do I set the glyphs emission color to save and load correctly ?
- Assistant
- User
It says it cannot find the material on the gameobject, but I assume it because the renderer is on the children
- Assistant
- User
The bool is not being loaded
public void LoadData(GameData data) { currentOrder = data.currentOrder; //glyphs = data.glyphs; // Load glyphs state foreach (var glyphData in data.glyphsState) { // Find the corresponding Glyph object in your scene or list var glyph = glyphs.Find(g => g.symbolType == glyphData.Key); if (glyph != null) { glyph.isActive = glyphData.Value;
// Update the visual state of the glyph based on the loaded isActive value //UpdateGlyphDisplay(glyph.symbolGlyph, glyph.isActive); } } } - Assistant