Make sure you have at the begining this:
DATA STORAGE:Code:using System.Collections.Generic; using Oxide.Core;
SET THE DATA ON PLUGIN LOAD:Code:class StoredData { //add a variable named "playervariable" to the StoredData Class (F.E: Kills) public Dictionary<ulong, double> playervariable = new Dictionary<ulong, double>(); } StoredData storedData;
READ THE DATA:Code:void Loaded() { //set storedData so it can be read or modified storedData = Interface.Oxide.DataFileSystem.ReadObject<StoredData>("DATAFILENAME"); }
You can make a variable to be 0 by default by using this code:Code:... double _playervariable; //THE VARIABLE THAT WILL GET THE VALUE OF STORED VARIABLE (OUT _PLAYERVARIABLE) if (storedData.playervariable.TryGetValue(playerID, out _playervariable))//SEE IF DATA IS NULL (EXISTS) { //if the data exists do something with it storedData.playervariable[playerID] = _playervariable + 1; //INCREASE THE VARIABLE BY 1; Interface.Oxide.DataFileSystem.WriteObject("DATAFILENAME", storedData); //SAVE THE VARIABLE(s) TO DATAFILE NAMED DATAFILENAME return; } //IF DATA IS NULL; (PLAYER HAS NO VALUE FOR A VARIABLE) storedData.playervariable[playerID] = 0; //SETS THE VARIABLE TO 0 Interface.Oxide.DataFileSystem.WriteObject("DATAFILENAME", storedData); //SAVE THE VARIABLE TO DATAFILE NAMED DATAFILENAME return;
Code:void OnPlayerInit(BasePlayer player) { double _playervariable; //THE VARIABLE THAT WILL GET THE VALUE OF STORED VARIABLE (OUT _PLAYERVARIABLE) if (storedData.playervariable.TryGetValue(playerID, out _playervariable)) return;//SEE IF DATA IS NULL (EXISTS) storedData.playervariable[playerID] = 0; //SETS THE VARIABLE TO 0 Interface.Oxide.DataFileSystem.WriteObject("DATAFILENAME", storedData); //save it }
Storing data in C#
Discussion in 'Rust Development' started by TheMechanical97, Jan 25, 2016.

I learned this code from other plugins ^^