Hi I've got a file datafile and I need to read it so I can retrieve the value of a key.
My file contains:Code:PlayerData data = Interface.Oxide.DataFileSystem.ReadObject<PlayerData>($"DataFolder/{playerId}");
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?Code:{ "id": 76561197xxxxxx, "name": "Playername", "key1string": "stringvalue1", "key2int": intvalue2, "key3int": intvalue3, "key4string": "stringvalue4" }
I'm using:
if KEY = key1string... return "stringvalue1"Code:private object GetDataInfo(ulong playerId, string KEY) { //basically I want this if data.KEY return data.KEYvalue }
Do I have to add the whole data to a list and use the list to get pair(key/value)?
Getting value from a key in custom class?
Discussion in 'Rust Development' started by FireBurst, Apr 29, 2018.
-
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; }
-
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) -
Code:void Loaded() { playerData = Interface.Oxide.DataFileSystem.ReadObject<Dictionary<string, PlayerClass>>("PlayerData"); }
-
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 -
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. -
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. }
Code:private double Balance(string playerId) { double playerData; return storedData.Balances.TryGetValue(playerId, out playerData) ? playerData : config.StartAmount; }
Code:playerData.$"{id}".TryGetValue(key, out data)
Code:{ "765611979xxxxx": { "id": 765611979xxxxx, "name": "FireBurst", "wood": 0, "stone": 10, ...... } }
Last edited by a moderator: Apr 30, 2018