sorry, but i think you should not have tried that in your " 1000 lines game code "
instead better in a extra little learning project.
and now, as you have more questions, you could just copy paste
that short ( but “runnable” ) code here and we could help you.
but a other point:
i play with something different and see that it can not be used exactly for your question,
still it deals with 2 things you might need:
- a user interface for get user name ( and password ) and show login status
- a settings file for that name / password and many more default program settings
- and deal with the problem that when the file not exists the settings are in the program,
written as default to ( new created ) file…
so you are able just to copy the code ( without a file ) and the user start it up as initial default…
if you are interested?
settings file example v02
// make a default processing app with settings file
// but if it not exists, it will be generated.
// usually i like the TABLE way, but here try a different approach: string dict
// now need add a String like a user name / password
// but incl a way for the operator to type it in
// v0.2 take create_settings call out of if show
// OPEN: a settings editor
String settingsfilename ="data/settings.txt";
StringDict setting;
String[] lines;
int clines;
//_____________________________________________________ check file exists and create file on error
BufferedReader reader;
PrintWriter writer;
//_____________________________________________________ operator input for test user...
import javax.swing.JOptionPane;
String answer, title = "input:", question="pls give me some words";
boolean userok= false, passwdok=false;
boolean show = true; // diag
//_____________________________________________________
void setup() {
size(300, 300);
load_settings(); // check if file exists, if not, it will create a empty file ( and even data/ dir )
// on empty file default settings will be loaded and saved
login();
}
//_____________________________________________________
void draw() {
background (sett_i("bg_R"), sett_i("bg_G"), sett_i("bg_B"));
fill(0);
if ( userok && passwdok ) text("hallo "+sett_s("user"), width-80, height-10);
else text(" not login ", width-80, height-10);
fill (sett_i("rect_fill_R"), sett_i("rect_fill_G"), sett_i("rect_fill_B"));
stroke (sett_i("rect_strk_R"), sett_i("rect_strk_G"), sett_i("rect_strk_B"));
strokeWeight(sett_i("rect_strk_W"));
rect (sett_i("rect_x"), sett_i("rect_y"), sett_i("rect_w"), sett_i("rect_h"));
}
//_____________________________________________________
int sett_i(String skey) {
return int(setting.get(skey));
}
//_____________________________________________________
String sett_s(String skey) {
return setting.get(skey);
}
//_____________________________________________________
void create_default_settings() {
if (show) println("if empty file we need start with defaults settings");
setting = new StringDict();
setting.set("bg_R", "200");
setting.set("bg_G", "200");
setting.set("bg_B", "0");
setting.set("rect_x", "10");
setting.set("rect_y", "10");
setting.set("rect_w", "20");
setting.set("rect_h", "30");
setting.set("rect_fill_R", "0");
setting.set("rect_fill_G", "200");
setting.set("rect_fill_B", "0");
setting.set("rect_strk_R", "0");
setting.set("rect_strk_G", "0");
setting.set("rect_strk_B", "200");
setting.set("rect_strk_W", "3");
setting.set("user", "");
setting.set("passwd", ""); // later should use md5..
}
//_____________________________________________________
void write_settings() { // write each setting to file using lines: varablename=variablevalue
String[] theKeys;
String theKey = "";
String theValue = "";
int clines = setting.size();
theKeys = setting.keyArray();
if (show) println("dict: setting, entries: "+clines);
lines = new String [clines];
for ( int i = 0; i < clines; i++ ) {
theKey = theKeys[i];
theValue = setting.get(theKey);
lines[i] = theKey+"="+theValue;
if (show) println(lines[i]);
}
saveStrings(settingsfilename, lines);
}
//_____________________________________________________
void create_settings() { // we found lines in settings.txt file but need to split and save to string dict.
setting = new StringDict();
for (int i = 0; i < clines; i++) {
String[] lvals = split(lines[i], '=');
setting.set(lvals[0], lvals[1]);
}
write_settings();
}
//_____________________________________________________
boolean file_exists() {
boolean isok = false;
reader = createReader(settingsfilename);
if ( reader == null ) {
if (show) println("problem: no file: "+settingsfilename);
// try to write one
writer = createWriter(settingsfilename);
writer.flush();
writer.close(); // wow, that even creates a not existing /data/ dir
if (show) println("created: pls. check: "+settingsfilename);
} else {
isok = true;
}
return isok;
}
//_____________________________________________________
void load_settings() {
if ( file_exists() ) if (show) println("file ok ");
lines = loadStrings(settingsfilename);
clines = lines.length;
if (show) {
println("read file: "+settingsfilename);
println("lines: "+clines);
for (String l : lines) println(l);
}
if ( clines == 0 ) create_default_settings();
else create_settings();
}
//_____________________________________________________
String textwindow() {
answer = JOptionPane.showInputDialog(null, question, title, JOptionPane.QUESTION_MESSAGE);
answer = trim(answer);
if ( answer == null ) answer = "";
if (show) println(answer);
return answer;
}
//_____________________________________________________
void ask_name(boolean writeit) {
String instr = "";
userok= false;
passwdok=false;
question="pls give me your name";
instr = textwindow();
if ( writeit ) {
setting.set("user", instr);
userok = true;
} else { // check correct
if ( instr.equals(setting.get("user")) ) userok = true;
}
question="pls give me password";
instr = textwindow();
if ( writeit ) {
setting.set("passwd", instr);
passwdok = true;
} else { // check correct
if ( instr.equals(setting.get("passwd")) ) passwdok = true;
}
if ( writeit ) write_settings();
}
//_____________________________________________________
void login() {
if ( sett_s("user").equals("") ) ask_name(true); // get name and password and write to settings file
else ask_name(false); // get name and password and check
if (show) println("user "+sett_s("user")+" pw "+sett_s("passwd"));
if (show) println("login: user "+userok+" pw "+passwdok);
}
//_____________________________________________________
have fun!
if you want, there is a bigger version at my blog,
http://kll.engineering-news.org/kllfusion01/articles.php?article_id=154#here16