- Home /
Save data to a location without override
so i have this script which enables me to save data in the following manner in a json file [name , score,time] the only issue is that after the data is saved and i run the program again it overrides the previous contents , what i want is that a new file should be created every time or the new contents should be saved one after the another in the same file . But override should be avoided . any ideas ?. the script is attached down below .
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class savegameformaterial1 : MonoBehaviour {
static readonly string SAVE_FILE = "new.json";
void Start () {
GameObject timerobject = GameObject.Find("sphere");
helpmetimer htimer = timerobject.GetComponent<helpmetimer>();
helperclass data = new helperclass() { name = "ravi", score = 0, time = htimer.timerText.text };
string json = JsonUtility.ToJson(data);
Debug.Log(json);
string filename = Path.Combine(Application.persistentDataPath, SAVE_FILE);
if (File.Exists(filename))
{
File.Delete(filename);
}
File.WriteAllText(filename, json);
Debug.Log("Player saved to" + filename);
}
void Update()
{
GameObject timerobject = GameObject.Find("sphere");
helpmetimer htimer = timerobject.GetComponent<helpmetimer>();
helperclass data = new helperclass() { name = "billiejoe", score = score4.scoreValue,time= htimer.timerText.text };
string json = JsonUtility.ToJson(data);
Debug.Log(json);
string filename = Path.Combine(Application.persistentDataPath, SAVE_FILE);
if (File.Exists(filename))
{
File.Delete(filename);
}
File.WriteAllText(filename, json);
Debug.Log("Player saved to" + filename);
}
}
Answer by tormentoarmagedoom · May 08, 2018 at 07:45 AM
Good day.
I see yo uare doing this at the start, and every frame in the update, this means you are saving this info every 0.05 seconds aprox! Why¿? is too much!.
First, do something to save every 5 seconds for example, should be enought!
Debug.Log(json);
string filename = Path.Combine(Application.persistentDataPath, SAVE_FILE);
if (File.Exists(filename))
{
File.Delete(filename);
}
File.WriteAllText(filename, json);
Debug.Log("Player saved to" + filename);
Is deleting your file, and createing a new one with the new information.
When you really need to save? every second? How many saved files you want? You can save the files with the date/hou in the name, using DateTime.Now.
So new file will be created every time. But i say again, if want "autosave" execute the save functions every 5 or 10 seconds, not multiple times every second!
Bye!
Yes, that is a good approach.
An alternative to adding the date (so that you don't run out of disk space) is to keep a limited number of save files, for example 5 or 10, and keep looping index. Something like this:
[SerializeField] private int maxSaveFileCount = 10;
private int currentFileIndex = 0;
...
// inside the saving function
currentFileIndex = (currentFileIndex + 1) % maxSaveFileCount;
string filename = Path.Combine(Application.persistentDataPath, SAVE_FILE + currentFileIndex);
// the rest is the same
this is also doing overrides in the same file , what i want is that all the previous values and the new updations to be saved in one file OR separate files . like a database but no overrides.
Of course it is overwriting previous files, that is to prevent filling your disk with save files (it is similar to log rotation technique). In case disk space is not important, then just use tormentoarmagedoom's solution of adding the date to the save file name.
Or you can use File.AppendText() ins$$anonymous$$d of File.WriteAllText()
.
this approach is good but frankly there is no need to go this deep . just wanna keep track of the values whenever i start and stop the application. either new file can be created every-time or the new as well as all the subsequent values can be kept in one file space is not the issue since the number of files is gonna be meagre. and time wise also there is no bound , i don't have to save file every second or so i just have to save the file(either updations or in a new file )every-time i start and pause the application.
The saving code is in an Update()
function, which runs every frame. If your application runs at 60 FPS, with the above code you will create 60 files every second. So the number of files will not be meagre (although their size will be small - but they will still take a full block of disk space).
To save at application start and end only, move the saving code into a Save()
function, and call it in Start()
and in OnApplicationQuit().
ok .i've added the onapplicationquit() function and save function inside it . how to add the dates OR how to make separate files each time at the end of the application?