- Home /
Loading from multiple files?
Hello! I'm making a game, and I've already done a simple file-io loading system with text files. How would I make loading slots? Like if the player selects slot 2 it'll only choose the loading files "Slot 2" on them.
Answer by Catlard · Dec 18, 2012 at 04:41 PM
Well, you're loading the file from a path, right? Just save the save data in different files based on the number of save slots you have. Don't try to put them all in the same one! So your save data .txt file (or whatever kind of data file it is) would be called save2.txt. You can even construct the path based on the int number of the save file. Just take the path string and + saveSlotNumber.ToString() + .txt. Bammo! Done.
Sorry for the long delay! So, I think what you need is to generate the path for the saving text file. I've updated my answer with a script that I have writes text save files. Basically, you need to use a C# StreamReader/StreamWriter or TextReader or TextWriter. Steamreaders and writers can append, but are slightly more costly than TextReaders/Writers. Basically, the other thing you can do is, once you've declared you're "using" the correct namespace (see my script), you can search for the text file of the right number (Check if File.Exists(Save1Path), and so on). It would probably be easiest to limit the number of save slots, so you can just overwrite the states when you need to make a new one or save new data. Hope this helps!
using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Collections.Generic;
public class AnnotationWriter : SingletonMonoBehaviour<AnnotationWriter>{
private StreamWriter _sW;
public void RecordAnnotationData(List<Annotation> anns) {
StartRecordingAnnotations();
if(anns.Count > 0) {
foreach(Annotation ann in anns)
RecordAnnotation(ann);
}
_sW.Flush();
_sW.Close();
}
public void DeleteAllAnnotationData() {
File.Delete(BuildManager.instance.GetFilePath(BuildManager.FileType.AnnotationData));
}
private void StartRecordingAnnotations() {
string annotationDataPath = BuildManager.instance.GetFilePath(BuildManager.FileType.AnnotationData);
if(File.Exists(annotationDataPath)) {
print ("Found the data file at " + annotationDataPath + ". Adding new data.");
_sW = new StreamWriter(annotationDataPath, true);
} else {
_sW = new StreamWriter(annotationDataPath);
print ("Creating a new data file, because a previous one was not detected.");
}
}
public void RecordAnnotation(Annotation ann) {
_sW.WriteLine();
_sW.WriteLine(ann._name);
_sW.WriteLine(ann._explorableTextName);
_sW.WriteLine(ann._tabName);
_sW.WriteLine(ann._type);
_sW.WriteLine(ann._startUVCoords);
_sW.WriteLine(ann._endUVCoords);
_sW.WriteLine(ann._entranceTime);
_sW.WriteLine(ann._timeToAppear);
_sW.WriteLine(ann._movementStyle);
_sW.WriteLine(ann._color);
_sW.WriteLine(ann._fullScale);
_sW.WriteLine(ann._localPosition);
_sW.WriteLine(ann._explorableTextLink);
_sW.WriteLine(ann._tabLink);
}
public void ClearData() {
}
}
Well how would I tell how many already exist? That's what I'm trying to do save1.txt save2.txt save3.txt and so on, but since it's going to create the slots as it goes ins$$anonymous$$d of having a set number how would I be able to get how many slots there already are, and number them properly?
Answer by Next Beat Games · Dec 18, 2012 at 05:10 PM
Not quite sure what you are trying to do. Do you mean you would like multiple profiles to save and load game data? Why not use PlayerPrefs?
Stay Tuned for the release of "Scratch Island" on mobile platforms
I'm asking is if I have a folder with multiple save files EX: save1.txt save2.txt save3.txt. How would I make it so it looks through and selects the one that the player selects? and save them according to how many are currently in there and dont have the same name
Your answer
Follow this Question
Related Questions
Load function not working propperly? 1 Answer
Saving items to a text file 1 Answer
Spawning random terrain pieces 1 Answer
Android File-io? 1 Answer
Android Multitouch 0 Answers