We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Saving and Loading .txt(s)
Page Index Toggle Pages: 1
Saving and Loading .txt(s) (Read 1001 times)
Saving and Loading .txt(s)
Mar 25th, 2010, 10:50am
 
I've written a sketch that uses loadStrings() to load .txts into strings, then I'm filling an array of integers with the converted strings.

I'm also using saveStrings() (doing everything in reverse) to save the data.

The sketch is basically a map making kind of thing (for a game).

When I run it through Processing it works perfectly, but when I export it, the .jar doesn't work correctly. (all it does is draw the first rectangle).

here's some of the code:

PImage[] animatedTiles = new PImage[33];
PImage[] floorTiles = new PImage[86];
PImage[] unpassableTiles = new PImage[108];
PImage[] trees = new PImage[29];
PImage[] houses = new PImage[51];
String[] instanceString = new String[80];
String[] typeString = new String[80];
int[] instanceInt = new int[80];
int[] typeInt = new int[80];
int[] cellPos = new int[80];
Tile[] tile = new Tile[80];

int mouseInstance;
int mouseType;
int time;

PFont font;

void setup(){
size(480,432);
font = loadFont("text-48.vlw");
textFont(font,36);
String[] instanceString = loadStrings("data/instance.txt");
String[] typeString = loadStrings("data/type.txt");
for(int i=0; i<80;i++){
cellPos[i] = i;
instanceInt[i] = int(instanceString[i]);
typeInt[i] = int(typeString[i]);
tile[i] = new Tile(cellPos[i],instanceInt[i],typeInt[i]);
}
frameRate(25);
loadTiles();
}

void draw(){
background(225);
rect(0,0,320,256);
text("rect",320,0);
for(int i=0;i<80;i++){
tile[i].draw();
}
text("loaded",0,0);
editorGrid();
drawMouse();
time++;
}

void loadTiles(){
for(int i=0;i<animatedTiles.length;i++){
animatedTiles[i] = loadImage("animatedTiles/"+i+".png");
}
for(int i=0;i<floorTiles.length;i++){
floorTiles[i] = loadImage("floorTiles/"+i+".png");
}
for(int i=0;i<unpassableTiles.length;i++){
unpassableTiles[i] = loadImage("unpassableTiles/"+i+".png");
}
for(int i=0;i<trees.length;i++){
trees[i] = loadImage("trees/"+i+".png");
}
for(int i=0;i<houses.length;i++){
houses[i] = loadImage("houses/"+i+".png");
}

}

What can I do to get it to work outside of processing? Thanks for the help (If you need more of the code to help me, just tell me).
Re: Saving and Loading .txt(s)
Reply #1 - Mar 25th, 2010, 12:05pm
 
I am surprised by loadStrings("data/instance.txt");
Do you have a data dir inside the data folder?

You export as applet or as application?
Were do you save the strings? They cannot be saved back to the data folder since it will be in the jar. So if you must read them back, you must have a different path.
Re: Saving and Loading .txt(s)
Reply #2 - Mar 25th, 2010, 12:35pm
 
so first things first: for what ever reason, loadStrings("instance.txt");
and
saveStrings("instance.txt", instanceString);
saves into the sketch folder by and not a data folder, do if I put a data/ it saves it into the data folder. I've checked it a few times.

I've exported as an applet and as an application. Neither work.

I have tried saving and loading the files from different locations. When I change the location to something outside the processing folder, it will load from the specified location. When it savesString, it makes a new folder inside the sketch's folder that has the same name as the folder I've loaded the Strings from.
So it seems it does not want to save outside the sketch folder.

Changing where it saves and loads doesn't seem to fix the application. It does however draw the firs tile now, but does nothing else correctly.
Re: Saving and Loading .txt(s)
Reply #3 - Mar 25th, 2010, 12:53pm
 
Quote:
for what ever reason

Well, that's because, as I wrote, the data folder goes inside the jar file, so Processing can't write there. Hence authors chose to save where your jar files are, as it is a known place and works on all systems.

Quote:
I've exported as an applet and as an application

My question was silly anyway, as an applet cannot write on disk (unless signed, then the problem of where to save remains).

Quote:
So it seems it does not want to save outside the sketch folder.

If you give an absolute path, it will save there.
Re: Saving and Loading .txt(s)
Reply #4 - Mar 25th, 2010, 1:27pm
 
I worked out how to get it to work on my computer.
which is: /Users/Jesse/Documents/Processing/mapEditor/application.macosx/instance.txt

but obviously this doesn't work once I move the folder or if someone else tries to use the application.

is there a way for me to get the application to change where it looks if the folder has been moved?
Re: Saving and Loading .txt(s)
Reply #5 - Mar 25th, 2010, 2:24pm
 
Perhaps you can read and write to an absolute path using sketchPath variable as a base.

I think you must distinguish:
- Reading initial data from the data folder (inside the jar, read-only).
- Saving the (modified) data, eg. on sketch path.
- Reading back the saved data. Eg. detect if it is there. If not, fall back to data folder path.
Re: Saving and Loading .txt(s)
Reply #6 - Mar 25th, 2010, 8:00pm
 
Now it works except for one thing. When I press up 3 times it stops working how it should. Normally the tile you have selected follow the mouse. Right now it when you press up the third time the tile stops where your mouse was. You can't get control of the tile again.
This problem does not show up when running it through Processing, only through the application. Here all of the code:

Code:
/*
Map Editor - by Jesse LloydDominik
*/

PImage[] animatedTiles = new PImage[33];
PImage[] floorTiles = new PImage[86];
PImage[] unpassableTiles = new PImage[108];
PImage[] trees = new PImage[29];
PImage[] houses = new PImage[51];
String[] instanceString = new String[80];
String[] typeString = new String[80];
int[] instanceInt = new int[80];
int[] typeInt = new int[80];
int[] cellPos = new int[80];
Tile[] tile = new Tile[80];

int mouseInstance;
int mouseType;
int time;

PFont font;

void setup(){
size(480,432);
font = loadFont("text-48.vlw");
textFont(font,36);
String[] instanceString = loadStrings("inProgress/instance.txt");
String[] typeString = loadStrings("inProgress/type.txt");
for(int i=0; i<80;i++){
cellPos[i] = i;
instanceInt[i] = int(instanceString[i]);
typeInt[i] = int(typeString[i]);
tile[i] = new Tile(cellPos[i],instanceInt[i],typeInt[i]);
}
frameRate(25);
loadTiles();
}

void draw(){
background(225);
rect(0,0,320,256);
text("rect",320,0);
for(int i=0;i<80;i++){
tile[i].draw();
}
text("loaded",0,0);
editorGrid();
drawMouse();
time++;
}



void editorGrid(){
fill(255);
rect(0,height-64,64,64);
rect(96,height-64,64,64);
rect(96+96,height-64,64,64);
fill(0);
textFont(font,20);
text("Load",6,height-36,64,64);
text("Save",6+96,height-36,64,64);
textFont(font,18);
text("Final",4+96+96,height-36,64,64);
for(int i = 0; i<320; i+=32){
 for(int j = 0; j<256; j+=4){
   point(i,j);
 }
}
for(int i = 0; i<256; i+=32){
 for(int j = 0; j<320; j+=4){
point(j,i);
 }
 }  
}

void keyPressed(){
if(key==CODED){
 if (keyCode == UP && mouseType<4){
   if (mouseType==0){mouseInstance=0;}
   if (mouseType==1 && mouseInstance>unpassableTiles.length-1){mouseInstance=0;}
   if (mouseType==2 && mouseInstance>trees.length-1){mouseInstance=0;}
   if (mouseType==3 && mouseInstance>houses.length-1){mouseInstance=0;}
   mouseType++;
 }
 else if (keyCode==DOWN && mouseType>0){
   if (mouseType==1 && mouseInstance>floorTiles.length-1){mouseInstance=0;}
   if (mouseType==2){mouseInstance=0;}
   if (mouseType==3 && mouseInstance>unpassableTiles.length-1){mouseInstance=0;}
   if (mouseType==4 && mouseInstance>trees.length-1){mouseInstance=0;}
 mouseType--;
 }
 else if (keyCode==RIGHT && mouseType==0 && mouseInstance<floorTiles.length-1){
 mouseInstance++;
 }
   else if (keyCode==RIGHT && mouseType==1 && mouseInstance<animatedTiles.length-1){
 if (mouseInstance==0){
 mouseInstance=3;
 }
 else if (mouseInstance==3){
 mouseInstance=6;
 }
 else if (mouseInstance==6){
 mouseInstance=10;
 }
 else if (mouseInstance==10){
 mouseInstance=14;
 }
 else if (mouseInstance==14){
 mouseInstance=18;
 }
 else if (mouseInstance==18){
 mouseInstance=21;
 }
 else if (mouseInstance==21){
 mouseInstance=24;
 }
 else if(mouseInstance==24){
 mouseInstance=27;
 }
 else{
 mouseInstance=30;
 }
 }
   else if (keyCode==RIGHT && mouseType==2 && mouseInstance<unpassableTiles.length-1){
 mouseInstance++;
 }
   else if (keyCode==RIGHT && mouseType==3 && mouseInstance<trees.length-1){
 mouseInstance++;
 }
   else if (keyCode==RIGHT && mouseType==4 && mouseInstance<houses.length-1){
 mouseInstance++;
 }
 else if(keyCode==LEFT && mouseInstance>0){
 if (mouseType==1){
   if (mouseInstance==30){
 mouseInstance=27;
 }
 else if (mouseInstance==27){
 mouseInstance=24;  
 }
 else if(mouseInstance==24){
 mouseInstance=21;
 }
 else if(mouseInstance==21){
 mouseInstance=18;
 }
 else if (mouseInstance==18){
 mouseInstance=14;
 }
 else if (mouseInstance==14){
 mouseInstance=10;
 }
 else if (mouseInstance==10){
 mouseInstance=6;
 }
 else if (mouseInstance==6){
 mouseInstance=3;
 }
 else{
 mouseInstance=0;
 }
 }
 else{
   mouseInstance--;
 }
 }
}
}

void mousePressed(){
edit();
 if (mouseX>96 && mouseX<96+64 && mouseY>height-64 && mouseY<height){
for(int i=0;i<80;i++){
instanceString[i] = str(instanceInt[i]);
typeString[i] = str(typeInt[i]);
}
saveStrings("inProgress/instance.txt", instanceString);
saveStrings("inProgress/type.txt", typeString);
}

if (mouseX>96+96 && mouseX<96+96+64 && mouseY>height-64 && mouseY<height){
for(int i=0;i<80;i++){
instanceString[i] = str(instanceInt[i]);
typeString[i] = str(typeInt[i]);
}
saveStrings("final/instance.txt", instanceString);
saveStrings("final/type.txt", typeString);
}  


if (mouseX>0 && mouseX<64 && mouseY>height-64 && mouseY<height){
String[] instanceString = loadStrings("inProgress/instance.txt");
String[] typeString = loadStrings("inProgress/type.txt");
for(int i=0; i<80;i++){
instanceInt[i] = int(instanceString[i]);
typeInt[i] = int(typeString[i]);
tile[i].in = instanceInt[i];
tile[i].t = typeInt[i];
}
}
}



void drawMouse(){
 if (mouseType==0){
   image(floorTiles[mouseInstance],mouseX,mouseY,32,32);
 }
 else if (mouseType==1){
 image(animatedTiles[mouseInstance],mouseX,mouseY,32,32);
 }
 else if(mouseType==2){
   image(unpassableTiles[mouseInstance],mouseX,mouseY,32,32);
 }  
 else if(mouseType==3){
  image(trees[mouseInstance],mouseX,mouseY,32,32);
 }
 else if(mouseType==4){
   image(houses[mouseInstance],mouseX,mouseY,32,32);
 }
 else{
 image(floorTiles[mouseInstance],mouseX,mouseY,32,32);
 }
}

Code finished in next post
Re: Saving and Loading .txt(s)
Reply #7 - Mar 25th, 2010, 8:02pm
 
Code:
void loadTiles(){
for(int i=0;i<animatedTiles.length;i++){
animatedTiles[i] = loadImage("animatedTiles/"+i+".png");
}
for(int i=0;i<floorTiles.length;i++){
floorTiles[i] = loadImage("floorTiles/"+i+".png");
}
for(int i=0;i<unpassableTiles.length;i++){
unpassableTiles[i] = loadImage("unpassableTiles/"+i+".png");
}
for(int i=0;i<trees.length;i++){
trees[i] = loadImage("trees/"+i+".png");
}
for(int i=0;i<houses.length;i++){
houses[i] = loadImage("houses/"+i+".png");
}
}

/*
This Class is used to handle the basic tiles. The tiles come in three types:
-Animated tiles
-Regular floor tiles
-Unpassable terain
*/


class Tile {
int n,in,t;
Tile(int number, int instance, int type){
in=instance;
t=type;
n=number;
}


void draw(){
if (t==0){
tFloor();
}
else if (t==1){
tAnimated();
}
else if(t==2){
tUnpassable();
}
else if(t==3){
tTrees();
}
else {
tHouses();
}
}

void tFloor(){
image(floorTiles[in],32*(n%10),32*((n-n%10)/10),32,32);
}
void tAnimated(){
if(in==6 || in==10 || in==14){
image(animatedTiles[((time-time%9)/9)%4+in],32*(n%10),32*((n-n%10)/10),32,32);
}
else{
image(animatedTiles[((time-time%9)/9)%3+in],32*(n%10),32*((n-n%10)/10),32,32);
}
}
void tUnpassable(){
image(unpassableTiles[in],32*(n%10),32*((n-n%10)/10),32,32);
}

void tTrees(){
image(trees[in],32*(n%10),32*((n-n%10)/10),32,32);
}

void tHouses(){
image(houses[in],32*(n%10),32*((n-n%10)/10),32,32);
}

}

void edit(){
for (int i=0; i<80; i++){
if (mouseX>=32*(i%10) && mouseX<32*(i%10+1) && mouseY>=32*((i-i%10)/10) && mouseY<32*((i-i%10)/10+1)){
instanceInt[i] = mouseInstance;
typeInt[i] = mouseType;
tile[i].in = mouseInstance;
tile[i].t = mouseType;
}
}
}


Thanks for the help.
Page Index Toggle Pages: 1