So i have managed to Create and add information to a dictionary and then alter the already existing data in there, however how would i achieve or is it possible to add new information to an existing key ?
So for exapmple i wanted to add to the end of this key bellow PID, lets say "rotation" : "13.93" ?
Something like #
if (key = 18814540){
add rotation
}
Thanks loads
Code:{ "Dictionary": { "18814540": { "DisplayName": "[MD] SniperSpool", "PlayerLocationx": "1298.168", "PlayerLocationy": "13.931", "PlayerLocationz": "975.916", "CID": "2822330", "PID": "76561198126816619" } } }
Adding information to Dictionary?
Discussion in 'Rust Development' started by SniperSpool, Jan 14, 2018.
-
Simple guide how to work with Dictionary.
Create dictionary:
Code:Dictionary<ulong, string> nameDictionary = new Dictionary<ulong, string>();
Code:nameDictionary.Add(key, value);
Code:if (!nameDictionary.ContainsKey(key)) { nameDictionary.Add(key, value); }
Better to check is key added already
Code:if (nameDictionary.ContainsKey(key)) { nameDictionary[key] = value; }
Better to check is key added already
[/CODE]if (nameDictionary.ContainsKey(key))
{
nameDictionary[key].Add(value);
}[/CODE]
Cause tValue of this Dictionary is List you should add value with same method. If you want to change value in dictionary which tValue is List, for e.x.
Code:nameDictionary[key][0] = "teststring";
-------------
In your situation tValue of your Dictionary is custom Class with info, something like
Code:class Custom { string PlayerLocationx; string PlayerLocationy; string PlayerLocationz; string CID; string PID; }
Code:class Custom { string PlayerLocationx; string PlayerLocationy; string PlayerLocationz; string PlayerRotation; string CID; string PID; }
Code:foreach (var check in BasePlayer.activePlayerList) { if (nameDictionary.ContainsKey(check.userId) nameDictionary[check.userId].PlayerRotation = check.transform.rotation.ToString(); }
Code:class Save{ string position; }
Later you can easy convert string to Vector3.
Code:player.Teleport(something[someKey].position.ToVector3());
Last edited by a moderator: Jan 14, 2018 -
Is there a way to List the tValue's of the key ?
Probably better for me to describe what im trying to do.
save a number of signs based on there NET.ID's. so i can for example.
"DisplayName": "[MD] SniperSpool",
"PlayerLocationx": "1298.168",
"PlayerLocationy": "13.931",
"PlayerLocationz": "975.916",
"CID": "2822330",
"PID": "76561198126816619"
--
"IMG1" : "NET ID , IMGINFO"
"IMG2" : "NET ID, IMGINFO"
--
i can then go through the list splitting the NET ID and Image info, looking at img1 to see if the NET id of save, matches the Net ID of the current component, if it does then do what i need it to do -
Ehrm, sorry here i can't help you, never worked hard on Signs. One questions, your final target see amount of signs? If yes - you dont need anything from this.
-
What im doing is playing with the Gyro copter from Colon Blow trying to make it persistent, ive managed to get it to save and respawn, in its last position, but odv as its being respawn, the code and images from the Signs are refreshed, i can get the image from the sign, and save it, however as they respawn they get new NETID's, so i therefor need a way to be able to get a new list of the NET ID's for all the signs on the copter so i can then save them and the images that go along with that.
if i can make a list of the sections within the key i can then check the values of those to see if they match and load the new ID's etc.
So to make a list of the keys i can use
List<String> ItemKeys = ChopperCache.Keys.ToList();
however im unsure how to get a list of the tvalue bellow each key. -
foreach (var check in myDictionary)
{
List<string> ItemKeys = check.Keys.ToList();
}
Anyway i can't understand why do you need list of Keys :/
Sorry, maybe language barrier. -
Save info
Code:var UniqueID = chairmount.net.ID; var ItemKey = Convert.ToInt32(Math.Round(Convert.ToDouble(UnityEngine.Random.Range(Convert.ToSingle(0), Convert.ToSingle(99999999))))).ToString(); if (!ChopperCache.ContainsKey(ItemKey)) { ChopperCache.Add(ItemKey, new ChopperInformation { DisplayName = player.displayName.ToString(), PlayerLocationx = player.transform.position.x.ToString(), PlayerLocationy = player.transform.position.y.ToString(), PlayerLocationz = player.transform.position.z.ToString(), ChopperID = UniqueID.ToString(), PID = player.userID.ToString() }); SaveData(); }
Code:var UniqueID = mountable.net.ID.ToString(); List<String> ItemKey = ChopperCache.Keys.ToList(); foreach (var ChopKey in ItemKey) { if (ChopperCache[ChopKey].ChopperID == UniqueID) { ChopperCache[ChopKey].PlayerLocationx = player.transform.position.x.ToString(); ChopperCache[ChopKey].PlayerLocationy = player.transform.position.y.ToString(); ChopperCache[ChopKey].PlayerLocationz = player.transform.position.z.ToString(); SaveData(); } }
---- ADDED ----
Forgot to add the bit when i spawn the new gyro
Code:List<String> ItemKeys = ChopperCache.Keys.ToList(); foreach (var ChopKey in ItemKeys) { var locxa = ChopperCache[ChopKey].PlayerLocationx; var locya = ChopperCache[ChopKey].PlayerLocationy; var locza = ChopperCache[ChopKey].PlayerLocationz; float locx = float.Parse(locxa); float locy = float.Parse(locya); float locz = float.Parse(locza); var spawnpos = new Vector3(locx,locy,locz); var PID = ChopperCache[ChopKey].PID; var OwnID = Convert.ToUInt64(PID); newCopter.OwnerID = OwnID; newCopter.Spawn(); ChopperCache[ChopKey].ChopperID = chairmount.net.ID.ToString(); SaveData(); }
Last edited by a moderator: Jan 14, 2018