I’m currently working on a saving system for my game, but it don’t work outside the editor. Here is my code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;
using LitJson;
using UnityEngine.UI;
public class CookingWriteJSON : MonoBehaviour {
public TextAsset CookingRecipes;
private TakeRecipe RecipesList = new TakeRecipe();
[System.Serializable]
public class TakeRecipe {
public List<RecipesFixed> recipes;
}
[System.Serializable]
public class RecipesFixed {
public string Name;
public string AtlasImage;
public bool Available;
public int Stock;
public bool Sweet;
public bool Sour;
public bool Bitter;
public bool Spicy;
public bool Starry;
public int DamagePoint;
}
JsonData RecipeJson;
void Start () {
RecipesList = JsonMapper.ToObject<TakeRecipe> (CookingRecipes.text);
MakeMealAvailable(0);
}
public void MakeMealAvailable (int mealID) {
RecipesList.recipes[mealID].Available = true;
RecipeJson = JsonMapper.ToJson (RecipesList);
File.WriteAllText (Application.dataPath + "/Resources/Cooking/RecipesFixed.json", RecipeJson.ToString());
}
public void BuyOneMeal (int mealID) {
RecipesList.recipes[mealID].Stock += 1;
RecipeJson = JsonMapper.ToJson (RecipesList);
File.WriteAllText (Application.dataPath + "/Resources/Cooking/RecipesFixed.json", RecipeJson.ToString());
}
public void UseOneMeal (int mealID) {
RecipesList.recipes[mealID].Stock -= 1;
RecipeJson = JsonMapper.ToJson (RecipesList);
File.WriteAllText (Application.dataPath + "/Resources/Cooking/RecipesFixed.json", RecipeJson.ToString());
}
void Update () {
TakeRecipe verify = JsonMapper.ToObject<TakeRecipe> (CookingRecipes.text);
Debug.Log(verify.recipes[mealID].Available);
}
}
I know EXACTLY where are the errors. It’s wheni do “File.WriteAllText (Application.dataPath + “/Resources/Cooking/RecipesFixed.json”, RecipeJson.ToString());” : the data path is not the same when exporting the project etc etc… What I was wondering is if you could do as I have done here : RecipesList = JsonMapper.ToObject (CookingRecipes.text);
I mean, having the json file as a textasset (Cookingrecipes) and loading it. Everything I’ve tried didn’t worked verry well - so I don’t know what do to now… Is there any easy and quick way to make it work for every platform ?
Also, I want to make sure that if the player close the game, the json file won’t reset and will keep the changes…
I’ve read that I do not have to specify that the file is on the Resource folder. Also, should I let the /cooking like it ? Because I keep having this error :
Because you are trying to save to the folder “cooking”, the folder doesn’t exist. You’ll need to create the folder first if you want to put it in there. I’m pretty sure I had to do that if I remember correctly.
Ok so I’ve just removed the cooking folder, so now the file is at the roots of the resources folder. There’s no more bug but it doesn’t modify the json file… Thank to these lines :
I think you misunderstand. The json file in the resource folder doesn’t get modified. You aren’t going to overwrite it. PersistantDataPath is a different path completely. But it’s used for all build targets and is different depending on your build target.
If you do a debug.log on persistantDataPath you’ll find where the file gets save at. I know where it is on windows, but it’s easier for you to debug it and see where it is.
Basically, you’ll check if the file exist when you load, and if so, load the modified file from the persistantDataPath. If not, load your template file from resources.
I’m sorry but I trully have some problems to understand you - english is not my main language so it’s not easy -. According to the debug.log, the file should be here : C:\Users\joaquim\AppData\LocalLow\Hishicorne\BeatOnTheMoon\beadonthemoon_Data\Resources
Yep, so, Application.persistantDataPath is a special location that you can save to that doesn’t require permissions usually from whatever your build target is (I think Android requires you add write external permissions into the manifest, but that’s no biggy).
The location is different for every build target.
Now, if you use File.Exist you can check if a file exist. If it returns false, you’ll just load up default values or whatever. If your file does exist, you’ll load it in through your textasset and json parser like normal.
If you want to view the file, you just need to go to the folder it’s stored in after saving. Just note that AppData folder is hidden, so make sure you set it where you can view hidden folders/files.
Yes nevermind I’ve just forgot something I knew I needed. It should be
RecipesList = JsonMapper.ToObject (File.ReadAllText(Application.persistentDataPath + “/RecipesFixed.json”));
Now… Everything “seems” work good. I just need to check if the json file is well saved and loaded. Thank a lot for your help and your patience !