- Home /
UnityScript for unique simpleJSON lists
What is the simplest way to save multiple lists of different variables in simpleJSON?
Im just getting started with learning JSON and this is what I've figured out so far
var Pets : List.<GameObject>;
function SavePets(){
var PetNames = new JSONObject(); var Petnames = new JSONArray();
var PetSpec = new JSONObject(); var Petspec = new JSONArray();
var PetLv = new JSONObject(); var Petlv = new JSONArray();
for(i =0; i < Pets.Count; i++){
Petnames.Add(Pets[i].GetComponent.<ptScript>().Name);
PetNames.Add(Petnames);
Petspec.Add(Pets[i].GetComponent.<ptScript>().Species);
PetSpec.Add(Petspec);
Petlv.Add(Pets[i].GetComponent.<ptScript>().Lv);
PetLv.Add(Petlv);
}
var path : String = Application.persistentDataPath + "PlayerSave.json";
File.WriteAllText(path, PetNames.ToString());
File.WriteAllText(path, PetSpec.ToString());
File.WriteAllText(path, PetLv.ToString());
}
This works, but I know there has to be a less tedious way of coding this. The game I'm making is similar to pokemon. The player will have tons of 'pets' each with their own stats. The method I'm posting creates 3 different lists containing each property of an individual pet object of what I want to save. I figure I could just iterate through each of the lists on load to grab a prefab of a pet the player has raised and repopulated the stats for it.
What I would like to do is instead of having a single list for each property I want to save, to make unique arrays for each pet with their own information
so instead of lists like
(rex, garfield, gumi, bojack)
(dog, cat, birb, horse)
(10, 12, 4, 20)
to make lists like
(rex, dog, 10)
(garfield, cat, 12)
(gumi, birb, 4)
(bojack, horse, 20)
The way I wrote it is the closest way I came to accomplish saving a list of random characters the player can collect.
this code is a VERY generic example. Each 'pet' in the game can have up to 30 different stats. Im only using simpleJSON to get away from using PlayerPrefs
Here is the JSON Im using https://github.com/Bunny83/SimpleJSON/blob/master/SimpleJSON.cs
thanks for any help
Your answer
Follow this Question
Related Questions
List problem? 2 Answers
An Array within array 2 Answers
A node in a childnode? 1 Answer
Accessing JSON in unityscript 0 Answers