Just wanted to provide a small little snippet of code for any developers that need assistance on creating a data file. I've commented on nearly every line for your convenience. There are hundreds of plugins to look at on the website if you want to take a closer look.
Note: This is mainly to help avoid those, "how do I create a data file" threads. Hopefully this should help out a tad bitCode:StoredData storedData; //Initalizing a reference for StoredData. So we can access the data within the class whenever we need without having to keep referencing it. This can be static if needed! //static StoredData storedData - An example of a static version of the above public class StoredData{ //Our main class. This is what we will store to the .json file! public int playerCount; //An exampe of an integer data type public bool booleanExample; //An example of a bool/boolean type public float damageperhit; //An example of a floating point data type/(float) public Dictionary<ulong, DataClass> players = new Dictionary<ulong, DataClass>(); //An example of a dictionary with a key(ulong) and a value of a class/data array public Dictionay<int, int> numbers = new Dictionary<int, int>(); //An example of a dictionary with an int as a key and a value public List<int> numb = new List<int>(); //An example of a list with the data type of int } //Ends our classpublic class DataClass{ //This is a class we are storing within a dictionary. This will save to data as defined above. //this is where you can really store whatever you want. For example player items, player health, player damage overtime, whenever you want. Even XYZ cords :D } //Ends our class public void SaveData() => Interface.Oxide.DataFileSystem.WriteObject("data file name here", storedData); //This is where we save our data //The first string is the file.json name. You should do either this.Title or a name related to your plugin so your users can see it. The second value is what we are saving. In this case we are saving 'StoredData' which we created above. Using our reference to it! public void LoadData(){ //Our function we are using to load data //The first value(storedData) is our reference to our main class we are using to save data. In our case storedData; //The second value within the <> is our actual class name. Which is what we are refering to, to save our data and what not(so StoredData in our case) //The last value which is a string is our 'Data File Name'. storedData = Interface.Oxide.DataFileSystem.ReadObject<StoredData>("data file name here"); //Note, this can be done within void Loaded() or void Init(). Either will work for our example. } //Example of using it within a init statement: void Init() => storedData = Interface.Oxide.DataFileSystem.ReadObject<StoredData>("data file name here"); //Simply directing init to this statement. It can still be done within the curly brackets. int GetPlayerCount(){ //A function that returns player count in user data var count = storedData.playerCount; //This is how we access our data. We are simply using our reference to dig within the class and grab the int playerCount; return count; //Returns the playerCount defined in storedData } void AddToPlayerCount(int x){ //We will be adding x to the player count with this function storedData.playerCount += x; //We add (x) to the player count in storedData SaveData(); //This is where we save our data so we can use it later //This will work the same with flat out setting it, dividing, subtracting, etc. } void CreateNewPlayerEntry(BasePlayer player){ //Here we will create a new data entry for the dictionary(players) if(storedData.players.ContainsKey(player.userID) return; //We are asking if the dictionary does contain this ID already. IF it does simply return and dont go forward. storedData.players.Add(player.userID, new DataClass()); //Adds a key to storedData.players called player.userID(your Steam 64 ID) - Then adds a new DataClass() type. SaveData(); //Saves the data }
Data File - Code Look Through for any unknowns?
Discussion in 'Rust Development' started by DylanSMR, Mar 22, 2017.