Ill start with im new to Plugins, and tbh pretty new to C, done visual basic in past, HTML and PHP.
Ive managed to save data to file, now i can get the Key from it but nothing beyond, how would i get to the Display Name, or PlayerLocation ? thanks all
Code:{ "chopperDictionary": { "76561198126816619": { "DisplayName": "[MD] SniperSpool", "PlayerLocation": "(257.8, 18.1, 1016.4)" } }
Reading data from datafile?
Discussion in 'Rust Development' started by SniperSpool, Jan 7, 2018.
-
When you rea d file from data to your Dictionary, (e.x. myDictionary<ulong, ClassName> - where ulong - key, and ClassName your calss name with DisplayName and PlayerLocation variables. You can use something like:
Code:void OnPlayerInit(BasePlayer player) { Puts(myDictionary[player.userId].DisplayName); Puts(myDictionary[player.userId].PlayerLocation); }
Vector3 pPosition = player.transform.position; // to get user Location
or
string pName = player.DisplayName; // to get user Name
You don't need any Dictionary.
Also if you use Dictionary don't forget to check, does this Dictionary have value with this key, with method like that:
Code:void OnPlayerInit(BasePlayer player) { if (myDictionary.ContainsKey(player.userId)) { Puts(myDictionary[player.userId].DisplayName); Puts(myDictionary[player.userId].PlayerLocation); } else { myDictionary.Add(player.userId, new MyClass()); Puts($"Added new info about {player.DisplayName}); } }
Last edited by a moderator: Jan 7, 2018 -
Thank you very much been driving me Nuts,
So im now grabbing the data from the DataFile by creating a new list to get keys, is there a better way of doing this ?
Code:[ChatCommand("read")] void cmdTest(BasePlayer player) { List<String> myKeys = cacheDictionary.Keys.ToList(); foreach (var test in myKeys) { Puts(cacheDictionary[test].DisplayName); Puts(cacheDictionary[test].PlayerLocation); SendReply(player, cacheDictionary[test].PlayerLocation); } }