Saving data to a .txt file

Hi, I am making a level editor for a game and have an idea on how to load level packs into the main game. The idea is to save data in the form of arrays of numbers that builds a level in a text document and then people can copy paste or download the text document and import it into a sub folder for the main game. So the problem is how could I create a manager for reading and writing the text documents in format. Lets say something like this:

Level 1: //resembles levels
    Layer 1: Time: 2.0 //spawn layers of enemies through the level, time is the delay for the next layer in seconds.
        1,200,400,1,0,25,2,2 //an array of numbers that resembles the data for an enemy x,y,size,speed,etc.
        1,800,300,-1,0,25,2,2
    Layer 2: Time: 5.0
        2,800,300,-1,0,25,2,2
        1,800,300,-1,0,25,2,2
Level 2:
    Layer 1: Time: 8.0
        5,23,424,12,5

And so on, so when they select a level pack (one of the .txt files from the sub folder) it will read the data like that. So how could I make it read and write data in a format like that, I am familiar with saving arrays to .txt files already it’s just I have no idea on how to make it write in that format and read it.

for me looks like spread sheet CSV file

level , layer , time , a,b,c,d,e,f,g,h,
1,1,2.0,1,200,400,1,0,25,2,2
//...
Table table;
String infilename = "data/game.csv";

/*
level , layer , time , a,b,c,d,e,f,g,h
 1,1,2.0,1,200,400,1,0,25,2,2
 */

String outfile = "data/newuser.csv"; 
int trows = 0, tcols;   

//____________________________________________________________
void setup() {
  size(500, 500);
  background(200, 200, 0);
  if ( true ) {
    get_Table();  //_____________________________________________ IF TABLE EXISTS READ
  }
  if ( true ) {
    make_Table();  //____________________________________________ EXAMPLE TABLE MAKE FILL WRITE
    make_Line();
    saveTable(table, outfile);
  }
}

//____________________________________________________________
void draw() {
  //  background(200,200,0);
}

//____________________________________________________________
void get_Table() {
  table=loadTable(infilename, "header");
  trows = table.getRowCount();
  tcols = table.getColumnCount();
  println("rows: "+trows+" columns: "+tcols+" in file: "+infilename);
  table.setColumnType("Level", Table.INT); // optional type setting...
  fill(0); //__________________________________________________________________ text color on canvas
  int dy =0;
  for ( int c = 0; c < tcols; c++)  text( table.getColumnTitle(c)+" ", 10+30*c, 10); //___ print header line
  dy++;
  for ( int r = 0; r < trows; r++) { //________________________________________ get data lines
    TableRow thisrow = table.getRow(r);
    for ( int c = 0; c < tcols; c++ ) text( thisrow.getString(c), 10+30*c, 10+15*dy );
    dy++;
  }
}

//_____________________________________________________________________________ options for table create / fill / save
void make_Table() {
  table = new Table();
  table.addColumn("level");
  table.addColumn("layer");
  table.addColumn("time");
  table.addColumn("a");
  table.addColumn("b");
  table.addColumn("c");
  table.addColumn("d");
  table.addColumn("e");
  table.addColumn("f");
  table.addColumn("g");
  table.addColumn("h");
}

void make_Line() {
  TableRow newRow = table.addRow();
  newRow.setInt("level", 1);
  newRow.setInt("layer", 1);
  //  newRow.setString("time", ""+year()+"_"+nf(month(), 2)+"_"+nf(day(), 2)+"_"+nf(hour(), 2)+"_"+nf(minute(), 2)+"_"+nf(second(), 2));
  newRow.setFloat("time", 2.0);
  newRow.setInt("a", 1);
  newRow.setInt("b", 200);
  newRow.setInt("c", 400);
  newRow.setInt("d", 1);
  newRow.setInt("e", 0);
  newRow.setInt("f", 25);
  newRow.setInt("g", 2);
  newRow.setInt("h", 2);
}

void keyPressed() {
  if ( key == 's' ) {
    saveTable(table, outfile);
    println("save to "+outfile);
  }
}


1 Like