I am trying to create a “midi” style system. i.e. Press a button and write the current time to a text file. Now this is simple but only if the whole thing is contained inside one function. Because the button can be pressed any number of times at any time I can’t figure out a way to put this inside on function. Also I can’t seem to declare the streamwriter object globally in the script because I get an error saying “IO Exception: Sharing violation on path …” But wherever I try to write the file I get the same problem unless the variable isn’t declared globally.
Here is a function which works perfectly but overwrites the file each time the key is pressed.
if (Input.GetButtonDown("rideBellKey")) {
var swRBell = new StreamWriter("/tmp/RideBell.txt");
swRBell.WriteLine(Time.time);
swRBell.Close();
}
This would be fine if I could append the file instead of overwrite it. I can’t fins any documentation on how to do this however. Here is the code which doesn’t work because of the variable being declared locally.
if (Input.GetButtonDown("recordKey")) {
if (!recording) {
var swRide = new StreamWriter("Ride.txt");
recording = true;
Debug.Log ("Started Recording");
} else {
swRide.Close();
recording = false;
Debug.Log ("Stopped Recording");
}
}
if (Input.GetButtonDown("rideKey")) {
swRide.WriteLine(Time.time);
}
Any help would be greatly apprieciated.
b.t.w. both of my examples are run in the “function Update () {…}” function.
I’m having the exact same problem!
Aha! I think I have our answer:
Okay, after looking over some stuff in the .NET framework, I’ve come up with this code:
import System;
import System.IO;
static var path : String;
static var fileName : String = "/play_session_data.txt";
static var myFile;
static function OutputLine (myText : String) {
WriteToFile.PrepareWriting();
File.AppendAllText(WriteToFile.path + fileName, myText + Environment.NewLine);
}
static function Output(myText : String){
WriteToFile.PrepareWriting();
File.AppendAllText(WriteToFile.path + fileName, myText);
}
static function PrepareWriting(){
WriteToFile.path = Application.dataPath;
if (!File.Exists(WriteToFile.path + fileName)){
myFile = File.CreateText(WriteToFile.path + fileName);
myFile.Close();
}
}
Just copy and paste this into a javascript script. Then call [ScriptName].Output(text : String) or [ScriptName].OutputLine(text : String) from anywhere in the game to append data to a file. Output() adds the text on the same line, whereas OutputLine() will add the text plus a new line character so that the next output will appear on the next line. The file will appear in the DATA folder after you make a build.
Hope this helps!
Thankyou very much dhturpin, but I’m sorry I just found how to append using streamwriter (the documentation is awful!).
Yeah sorry I have already found out how to append to a text file. I wasn’t going to post it on this thread till I had sorted the reading of the files afterwards (which I am working on). I just cleared/created the file when the user starts recording by using this.
I then found all you had to do was add a boolean variable onto the end of the file path string.
This above code just adds a new line onto the end of the text file. I just called that function every time the button was pressed.
This was very simple and easy to implement. Thankyou for the script though. I must say I’m a bit of a “noob” when it comes to javascript I only really understand “if” “else” functions. Making my own functions then calling back to them I still haven’t got the hang of. So I like to keep it all extremely simple :p.