Search Unity

[SOLVED] Monitor PlayerPrefs Wrapper?

Discussion in 'Editor & General Support' started by renman3000, Sep 1, 2020.

  1. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Hi there,
    Is there a way to know, a setting perhaps, a script, that will allow me to know when PlayerPrefs has been called and what command was called?

    I think I may be missing a deleteAll(). But I cant find it in my scripts.

    Thanks
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Are you using an IDE? You should be able to search your project for references to PlayerPrefs. If you're using Visual Studio try this:

    Edit -> Find and Replace -> Find in Files

    Then type "PlayerPrefs" and click "Find all". A window will pop up showing all references to PlayerPrefs in your project.
     
  3. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697

    Hi Man,
    Thanks!
    I did not know about this.... however, sadly, there is no DeleteAll()... this is frustrating since when I reboot the game, the data saved is not there.

    Errrr
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Is it possible you have mismatched key strings? For example "PlayerHealth" vs "Playerhealth". That sort of thing?
     
  5. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    No. Double, Triple Checked.

    Thing is, other projects work fine in this regard. Also, I have followed this Brackeys tutorial on how to save and load outside of PlayerPrefs. I have it fully integrated in my system, and it works, as long as I dont quit. If I do, and restart, the data is gone.

    So odd!



    I have pasted my code, if you understand this, can you see anything I am doing incorrectly?


    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.IO;
    4. using System.Runtime.Serialization.Formatters.Binary;
    5.  
    6.  
    7.  
    8.  
    9. public static class ProgressSave
    10. {
    11.  
    12.  
    13.  
    14.     public static void saveProgress(int level, int world)
    15.     {
    16.         BinaryFormatter formatter = new BinaryFormatter();
    17.         string path = Application.persistentDataPath + "/progress";
    18.  
    19.         FileStream stream = new FileStream(path, FileMode.Create);
    20.  
    21.         ProgressBinary progressBinary = new ProgressBinary(level, world);
    22.  
    23.         formatter.Serialize(stream, progressBinary);
    24.         stream.Close();
    25.  
    26.  
    27.     }
    28.  
    29.  
    30.     public static ProgressBinary getData()
    31.     {
    32.         Debug.Log("UWLP: SD: PB getData() = ");
    33.         string path = Application.persistentDataPath + "/progress";
    34.         if (File.Exists(path))
    35.         {
    36.             Debug.Log("UWLP: SD: PB path exists = ");
    37.             BinaryFormatter formatter = new BinaryFormatter();
    38.             FileStream stream = new FileStream(path, FileMode.Open);
    39.             ProgressBinary data =  formatter.Deserialize(stream) as ProgressBinary;
    40.             Debug.Log("UWLP: SD: PB path data = " + data.ToString());
    41.             stream.Close();
    42.             return data;
    43.  
    44.         }
    45.         else
    46.         {
    47.             Debug.LogError("UWLP: SD: PBX: FILE NOUT FOUND");
    48.             return null;
    49.  
    50.         }
    51.     }
    52.  
    53.  
    54. }
    55.  
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    I see only one issue with the particular code you shared here, which is that you're unsafely using filestreams. You should use a
    using
    statement to wrap your filesystem usage to make sure the filestream is closed properly even in the case of an exception during writing the file. That being said I don't think that issue would cause what you're seeing.

    The problematic code may be outside of the snippet you shared. For example, maybe you're saving the game data at the beginning of the game before loading anything, and therefore overwriting your save with empty data.
     
    renman3000 likes this.
  7. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697

    Thanks, I have got it working... the only thing I can think of, is the I removed the call to save() at OnDestroy(). Perhaps this, caused it to lose its references.


    Also, Visual Studio has a Find function for the entire project, that I was unaware of.
    Cheers