1. Hi!

    So I have the following in a data file created using Oxide's API:

    Code:
    {
      "PlayerData": [
        {
          "UserId": "76561198054069055",
          "TimesJoined": 0
        }
      ]
    }
    What is the best way to read from this then +1 to "TimesJoined" for the individual player in OnPlayerInit and write to the data file using Oxide's API?
     
    Last edited by a moderator: Jun 4, 2016
  2. Code:
    You could do it like: dataStuff.DataStuff[player.userID].TimesJoined = dataStuff.DataStuff[player.userID].TimesJoined + 1;
    SaveData(); //Or whatever your save function is
    [DOUBLEPOST=1465084389][/DOUBLEPOST]A example but this deals with gather-rate instead of data(Im sure you can change it up)
    Code:
    item.amount = (int)(item.amount * storedData.Player[player.userID].QuarryX);        
     
  3. I can't seem to get that to work, probably because I am using a Hashset instead of a dictionary or list.
    This is the code I use to create the datafile:

    Code:
    void SaveData() => Interface.Oxide.DataFileSystem.WriteObject("GUIAnnouncements_PlayerTimesJoined", storedData);
      
            void LoadSavedData()
            {
                storedData = Interface.Oxide.DataFileSystem.ReadObject<StoredData>("GUIAnnouncements_PlayerTimesJoined");
            }
      
            class StoredData
            {
                public readonly HashSet<PlayerTimesJoined> PlayerData = new HashSet<PlayerTimesJoined>();
            }
      
            class PlayerTimesJoined
            {
                public string UserId;
                public int TimesJoined;
          
                public PlayerTimesJoined()
                {
                }
          
                public PlayerTimesJoined(BasePlayer player)
                {
                    UserId = player.userID.ToString();
                    int TimesJoined;
                }
            }
      
            StoredData storedData;
            readonly HashSet<PlayerTimesJoined> PlayerData = new HashSet<PlayerTimesJoined>();
            void OnServerSave() => SaveData();void OnPlayerInit()
    {
        var Data = new PlayerTimesJoined(player);
        if(!storedData.PlayerData.Contains(Data))
        {
            storedData.PlayerData.Add(Data);
            SaveData();
        }
        else
        {
    //        Increment TimesJoined here.
            SaveData();
        }
    }
    Is it possible to do this with a Hashset? I would quite like to use it considering it is a very fast method of storing and reading data.
     
    Last edited by a moderator: Jun 5, 2016
  4. I have changed it to a dictionary for now and I have it working. It would still be interesting to know if there was a way to do it with a Hashset.
     
  5. Yeah I never really used a hashset as they we're a bit weird. I'm sure it similar to a dictionary maybe.
     
  6. JSON files are (mostly) just a single JSON object, which always contains key: value mappings. A hashset only contains values.
     
  7. So it wouldn't really be feasible for what I want to use it for.
     
  8. Exactly. A set is basically a list where all the elements are unordered, there is no such thing in JSON. There are numbers, strings, dictionaries/maps, booleans, lists/arrays (which are ordered) and null. A HashSet maps to neither of those, and JSON files are mostly just dictionaries/maps which contain string keys and JSON values as map values.