Hi!
So I have the following in a data file created using Oxide's API:
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?Code:{ "PlayerData": [ { "UserId": "76561198054069055", "TimesJoined": 0 } ] }
Solved Incrementing int in datafile? (C#)
Discussion in 'Rust Development' started by JoeSheep, Jun 4, 2016.
-
Code:
You could do it like: dataStuff.DataStuff[player.userID].TimesJoined = dataStuff.DataStuff[player.userID].TimesJoined + 1; SaveData(); //Or whatever your save function is
Code:item.amount = (int)(item.amount * storedData.Player[player.userID].QuarryX);
-
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(); } }
Last edited by a moderator: Jun 5, 2016 -
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.
-
-
-
-
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.