I am writing a program that contains a “save file” mechanic to it. What I want to do is offer the user of the program a chance to name the save file to whatever they wish. I believe I understand the gist of the steps. But I’m having some trouble understanding how I take in keyboard inputs from the user. Currently the program runs 100% on mouse click buttons.
The base idea I have is that there is some String filename that I create and that the user will assign a value to using the keyboard.
My question is, how to I log the keystrokes from the user effectively? I just want the user to be able to name a String in the program something like “My Save” or “Version 1”. I understand how I can turn that String value into a file directory where files get saved.
So, I do know selectOutput. I think that’s to call an existing file so that would be for my “load” functionality.
What I’m looking for is for the user to be able to input a string that will be the “save” filename.
My program tracks some information as you use it and stores that information in a spread sheet. But if you close and reopen the program all of those sheets reset. I want to allow the player to save progress with a filename of their choice.
You are right, I misread the processing documentation so thank you for the correction.
However, I’m not sure selectOutput() works with my existing code, or I’m possible not understanding it well enough. It still requires that a preexisting file be chosen from the hard drive. I want the user of my program to create a new file location that will be saved to.
A little more background. My program is built to track turns, dice rolls, and other in-game play information from a board game. I want this information saved so that I can create a save-progress function in my program to let the user stop mid game if desired. I currently have all of the information I need written into different .csv files in the draw() function. When you stop the program, those files have the stored information from your stop point. The problem is unless you manually move the .csv files to a new directory or change the file names in the code before running again, they just get reset when you run the program a second time.
My goal when I asked this question was to try to let the user create a new directory at the start of the program where the .csv files will go. That way I could eventually write a function that allows that .csv data to be loaded back into the program like a save game style file without the user needing to relocate files on their own machine.
The way I currently have this programmed, all I need is for a prompt to ask the player what to call the save file. Then I can just use something like this to create a directory for the saved data that I can reference later.
So, should I rewrite the existing code to save the data differently as the user proceeds through the program? Or, how can I prompt the user to assign a String value at the beginning of the program?
You can change the Header of the Save Dialog like “Save Your Chess Game” or whatnot
Consider to add a file extension like .txt when missing automatically
Remark
But, sure it is more elegant to let the user type in the name of the save file and the Sketch takes care of the folder location. Or just create a file name by player name + date + time + number of move or so.
I mean even the load file dialog provides too much stuff - you could just handle the list of save games yourself.
Chrisir
// Demo for selectOutput
int status=0;
String fileNameFromSave="";
void setup() {
size (600, 600);
// start our save dialog and connect it to the function fileSelected
selectOutput("Choose name and location to save:", "fileSelected");
}
void draw() {
// empty
background(0);
switch(status) {
case 0:
break;
case 1:
text("abort", 44, 44);
break;
case 2:
text("saved as "+fileNameFromSave, 44, 44);
break;
default:
text("Error 1638 ", 44, 44);
break;
}
}
// ------------------------------------------------------------------------------------------
void fileSelected(File selection) {
if (selection == null) {
println("Window was closed or the user hit cancel.");
status=1;
} else {
println("User saves as " + selection.getAbsolutePath());
String[] arrayTest = new String[1];
arrayTest[0]="Test 33";
saveStrings(selection.getAbsolutePath(), arrayTest);
status=2;
fileNameFromSave = selection.getAbsolutePath();
}//else
}//func
//