I am making a text adventure and whenever a player types "pick up the iron sword" or whatever, I need to be able to take the iron sword OUT of the JSON array within the room that it is kept in.
{
Rooms: [
{
id: "PizzaShop1",
name: "Pizza Shop",
description: "in a nice warm pizza shop that smells of garlic.",
items: [
{
id: "HawaiianPizza1",
name: "Hawaiian Pizza",
description: "a pizza topped with ham and pineapple",
strengthMod: 0,
toughnessMod: 0
},
{
id: "MeatloversPizza1",
name: "Meatlovers Pizza",
description: "a pizza topped with lots of meat",
strengthMod: 0,
toughnessMod: 0
}
],
entities: [
{
name: "Pizza Chef",
description: "an italian man",
friendly: true
},
{
name: "Mouse",
description: "a pesky mouse",
friendly: false
}
],
northExit: "",
southExit: "Road1",
eastExit: "",
westExit: ""
},
{
id: "Road1",
name: "Road",
description: "on a town road",
items: [
{
id: "IronSword1",
name: "Iron Sword",
description: "a battered but usable sword",
strengthMod: 2,
toughnessMod: 0
}
],
entities: [],
northExit: "PizzaShop1",
southExit: "",
eastExit: "",
westExit: ""
}
]
}
And here is my c# code:
else if (s.Contains(" pick up "))
{
String newJson = "";
s = s.Replace(" the ", " ");
s = s.Replace(" pick ", " ");
s = s.Replace(" up ", " ");
String[] Words = s.Split(' ');
foreach (String word in Words)
{
if (word != Words[1])
{
Words[1] = Words[1] + " " + word;
Words[1] = Words[1].Replace(" ", " ");
Words[1] = Words[1].Trim();
}
}
using (StreamReader sr = new StreamReader("TAPResources/map.json"))
{
String json = sr.ReadToEnd();
dynamic array = JsonConvert.DeserializeObject(json);
foreach (var rooms in array["Rooms"])
{
foreach (var item in rooms["items"])
{
String itm = (String)item["name"];
PrintMessage("Words: " + Words[1]);
PrintMessage("Item Name: " + itm.ToLower());
if (Words[1] == itm.ToLower())
{
rooms["items"].Remove(item);
}
}
}
newJson = (String)JsonConvert.SerializeObject(array);
}
File.WriteAllText("TAPResources/map.json", newJson);
}
The line:
rooms["items"].Remove(item);
Gives an error, because I can't edit the array within the loop. Normally I would solve this by adding the value to another array, then iterating through that array to remove from the initial array, but I don't know how to make an array for that variable type.