1. Hi I've got a file datafile and I need to read it so I can retrieve the value of a key.
    Code:
    PlayerData data = Interface.Oxide.DataFileSystem.ReadObject<PlayerData>($"DataFolder/{playerId}");
    My file contains:
    Code:
    {
      "id": 76561197xxxxxx,
      "name": "Playername",
      "key1string": "stringvalue1",
      "key2int": intvalue2,
      "key3int": intvalue3,
      "key4string": "stringvalue4"
    }
    There is no {namelist} before the {"id: .... } so I cannot use a list or dictionary (I suppose). Unfortunately I cannot use | data.Contains(), data.ContainsKey, data.TryGetValue() | stuffs... It seems that trying | pair.Value.data.ContainsKey(value) OR foreach (var pair in data) | don't work either. What would be the way to catch the value of a specific key?

    I'm using:
    Code:
    private object GetDataInfo(ulong playerId, string KEY)
            { //basically I want this
                if data.KEY
                return data.KEYvalue
          }
    
    if KEY = key1string... return "stringvalue1"

    Do I have to add the whole data to a list and use the list to get pair(key/value)?
     
    Last edited by a moderator: Apr 29, 2018
  2. I'm not 100% sure what your asking for, but here is how I would suggest going about it if your wanting to store data per player.
    Code:
            // Class that contains all of our information for the player
            public class PlayerClass
            {
                public string name;
                public string key1string;
                public int key2int;
                public int key3int;
                public string key4string;
            }        // Dictionary where the the key will be the userID and the value will be their data
            public Dictionary<string, PlayerClass> playerData = new Dictionary<string, PlayerClass>();        public PlayerClass GetPlayerData(string id)
            {
                // Define out player data so we can use TryGetValue
                PlayerClass data = null;
                // Lets try and get the data based on the id
                if (playerData.TryGetValue(id, out data))
                    return data; // We found the data, return it!
                return null; // Uh oh, no data was found. Lets just return null. You could also create a new data set and return that here if you wanted.
            }        public string GetPlayerName(string id)
            {
                // Get the data, that returns our PlayerClass. From here we just check if its null and return the name. Which is what the ? does!
                return GetPlayerData(id)?.name;
            }
     
  3. Thanks i'll see what I can do with that. I try to read the data file (ReadObject), should I replace
    PlayerClass data = null;
    with
    PlayerClass data = Interface.Oxide.DataFileSystem.ReadObject<PlayerData2>($"DataFolder/{id}");?
    Or should I use a Try and Catch (find data file, if none, make new Dictionary)?

    The current data is null, how could it find anything in there :s I'm a bit confused because when i use it it always return nothing.

    The data file is the playerid. in every data file is public string name, key1string, key2int etc. So it has to load the id of player and get whatever we want from it (in your example is the name)
     
  4. Unless each player has a large amount of data I wouldn't suggest storing each player in their own seperate file. Instead just store all players in one file to save on performance. For my example, you would want to just load the playerData dictionary using the DataFileSystem.ReadObject in the Loaded/Init function. Example:
    Code:
    void Loaded()
    {
    playerData = Interface.Oxide.DataFileSystem.ReadObject<Dictionary<string, PlayerClass>>("PlayerData");
    }
    Code above was not tested, wrote this in a text editor.
     
  5. Okay will do that. Is 20-30 keys/values per player good? I'm planning to use over 20 lines per player to store in the file. x100+ players would make it 2000+ lines in a single datafile. Is it still good for performance?

    I see most dictionaries with a string in it (<Dictionary<string, PlayerClass>>("PlayerData")), if I plan to use int, string, ulong, double etc does it matter? sorry about that stupid question xD
     
  6. Yes, the PlayerClass can contain whatever mix of variables types you like.
    If there's any chance of duplicates with that string key, you might want to use ulong userID instead.
    Storing an entry per player, with 20 or so vars each, in one data file shouldn't cause any problem.
     
  7. Thanks alot.

    One last thing the script works except the ?.name doesn't seem to work at all. It just returns because it finds the ID, if it does not, it simply don't return any name.

    I want to retrieve a value from specific key
    Code:
    public PlayerData GetPlayerData(string id, string key)
            {
                PlayerData data = Interface.Oxide.DataFileSystem.ReadObject<PlayerData>("data_file");
                // Lets try and get the data based on the id            if (playerData.TryGetValue(id, out data))
                {
                    >>I know following lines are incorrect but shows basically what I want
                  foreach (var pair in playerData)
                    {
                       if (pair.Key.Contains(key)) //I want to select the key from GetPlayerData(string id, string key), is a string
                            return pair.Value; //just want to return the value of previously selected Key
                    }
                }
                return null; // Uh oh, no data was found. Lets just return null. You could also create a new data set and return that here if you wanted.
            }
    I thought going like Economics script would be easier:

    Code:
    private double Balance(string playerId)
            {
                double playerData;
                return storedData.Balances.TryGetValue(playerId, out playerData) ? playerData : config.StartAmount;
            }
    Except my json data doesn't have Balances:{ in front line so I must replace Balances with PlayerID. Would look something like it:
    Code:
    playerData.$"{id}".TryGetValue(key, out data)
    datafile is :
    Code:
    {
      "765611979xxxxx": {
      "id": 765611979xxxxx,
      "name": "FireBurst",
      "wood": 0,
      "stone": 10,
    ......
      }
    }
     
    Last edited by a moderator: Apr 30, 2018