I'm getting JSON messages when trying to read the data on load.
Json ExampleCode:Failed to initialize plugin 'ServerStats v0.0.1' (JsonSerializationException: Unable to find a constructor to use for type Oxide.Plugins.ServerStats+PlayerStats. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path '76561198298734142.Logins', line 3, position 13.)
Code:{ "76561198298734142": { "Logins": 1, "LastLogin": "2017/07/13 08:17:43" } }
I'm learning here and not sure why its not working, anyone able to point me in the right direction?Code:class ServerStats : RustPlugin { Dictionary<ulong, PlayerStats> playerStats = new Dictionary<ulong, PlayerStats>(); class PlayerStats { public int Logins; public string LastLogin; public PlayerStats() { } internal PlayerStats(ServerStats serverstats) { Logins = 1; LastLogin = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"); } } void Loaded() { LoadData(); foreach (BasePlayer player in BasePlayer.activePlayerList) { if (!playerStats.ContainsKey(player.userID)) { playerStats.Add(player.userID, new PlayerStats(this)); SaveData(); } } } void LoadData() { playerStats = Interface.Oxide.DataFileSystem.ReadObject<Dictionary<ulong, PlayerStats>>("ServerStats/PlayerStats"); } void SaveData() { Interface.Oxide.DataFileSystem.WriteObject("ServerStats/PlayerStats", playerStats); } }
Issues reading saved data file using custom class
Discussion in 'Rust Development' started by Dubz, Jul 13, 2017.
-
Remove
Code:ServerStats serverstats
-
The following is just advice which I have comprised from working heavily with Oxide's data helpers. Firstly, I find it better to store a SteamID as a ulong inside the actual class then you can just make a list.
Code:List<PlayerData> players = new List<PlayerData>();
Code:PlayerData GetData(BasePlayer player) => players.FirstOrDefault(x => x.SteamID == player.userID);
Edit: Also, DateTime can be serialized. -
@Dubz I don't have any issues loading or saving data using the code you provided above -
-
I disagree with your opinion
Case: Checking if a item exists
Code:// Dictionary if(dict.ContainsKey(key)) return true;//List if (list.FirstOrDefault(x => x.var == someVar) != null) return true;
Code:// Dictionary Value value = null;if (dict.ContainsKey(key)) // Second Fastest { value = dict[key]; // Perform code } else { // Create new value }if (dict.TryGetValue(key, out value)) // Fastest { // Value found, perform code } else { // Create new value }// List value = list.FirstOrDefault(x => x.var == someVar); // Slowest out of these options if (value != null) { // Perform code } else { // Create new value }
-