Having trouble loading variables from a file
Hi, Gravity Games here, and I need to be able to load variables from a file so I can make different sized levels for my game. I'd prefer not to have to hard code anything, because I plan to have a level editor later on. So I NEED to be able to load the variables from a file.
This is the code from the java class that loads the levels:
Code :
package scorpioengine.main;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.lang.*;
import javax.swing.ImageIcon;
public class Level {
private Scanner l;
private Scanner lh;
private String Header[] = new String[5];
private String stringlevelsizex = "14";
private String stringlevelsizey = "14";
private String stringmusic = "1";
private String stringtileset = "1";
public void openHeader(){
try{
lh = new Scanner(new File("C://CWWLEVELS//world1stage1cfg.txt"));
}catch(Exception e){
System.out.println("error loading configuration file");
}
}
public void readHeader(){
while(lh.hasNext()){
for(int i=0; i < 1; i++){
stringlevelsizex=lh.next();
}
for(int i=1; i < 2; i++){
stringlevelsizey=lh.next();
}
for(int i=2; i < 3; i++){
stringmusic=lh.next();
}
for(int i=3; i < 4; i++){
stringtileset=lh.next();
}
}
}
public void closeHeader(){
lh.close();
}
private int levelsizex = Integer.parseInt(stringlevelsizex);
private int levelsizey = Integer.parseInt(stringlevelsizey);
private int music = Integer.parseInt(stringmusic);
private int tileset = Integer.parseInt(stringtileset);
private String Level[] = new String[(levelsizey)];
private Image grassek,
grassektop,
waterek,
path1,
path2;
public Level(){
ImageIcon img = new ImageIcon("C://CWWGFX//GrassmainEKlarge.PNG");
grassek=img.getImage();
img = new ImageIcon("C://CWWGFX//ChompPortrait.PNG");
grassektop=img.getImage();
img = new ImageIcon("C://CWWGFX//EKWaterLarge.PNG");
waterek=img.getImage();
img = new ImageIcon("C://CWWGFX//Path1Large.PNG");
path1=img.getImage();
img = new ImageIcon("C://CWWGFX//Path2Large.PNG");
path2=img.getImage();
openHeader();
readHeader();
closeHeader();
openFile();
readFile();
closeFile();
}
public Image getGrassEK(){
return grassek;
}
public Image getGrassEKTop(){
return grassektop;
}
public Image getWaterEK(){
return waterek;
}
public Image getPath1(){
return path1;
}
public Image getPath2(){
return path2;
}
public String getLevel(int x, int y){
String index = Level[y].substring(x,x+1);
return index;
}
public int getlevelsizex(){
return levelsizex;
}
public int getlevelsizey(){
return levelsizey;
}
public void openFile(){
try{
l = new Scanner(new File("C://CWWLEVELS//world1stage1.txt"));
}catch(Exception e){
System.out.println("error loading map");
}
}
public void readFile(){
while(l.hasNext()){
for(int i=0; i < ((levelsizey)); i++){
Level[i]=l.next();
}
}
}
public void closeFile(){
l.close();
}
}
...and here is the text in "world1stage1cfg":
I know its not the file itself, because I've tried the numbers with and without the quotation marks. If you know whats wrong, please help! I've tried everything, but I'm pretty sure the problem is that the open, read, and close Header routines aren't being run properly. Also, it runs fine when the defaults for the variables are the same as in the file, but obviously that won't always be the case.
Re: Having trouble loading variables from a file
Your catch block does not post informative information. If you instead print a stacktrace, what errors do you see? Also, are you sure that you want to use files? You might consider using resources instead, especially if any of the data will be held inside of a jar.
Re: Having trouble loading variables from a file
I'm not sure what you mean by stacktrace or resources, unless the stacktrace is the errors the Eclipse SDK shows, or by resources you mean placing the files in my workspace's res folder. Cause I tried replacing "println" with "stacktrace", but Eclipse gave me an error, and I plan to move the images I call to the res folder. If it helps, here is what Eclipse gives as an error when I change the initial values of "stringlevelsizex" and "stringlevelsizey" to "13" instead of "14":
Quote:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at scorpioengine.main.Level.readFile(Level.java:137)
at scorpioengine.main.Level.<init>(Level.java:88)
at scorpioengine.main.MainJPanel.<init>(MainJPanel.ja va:40)
at scorpioengine.main.MainWindow.main(MainWindow.java :19)
Sorry if I'm being a n00b and completely missing something, as I'm about an advanced beginner at best...
Re: Having trouble loading variables from a file
I mean using printStackTrace method of Exception:
Code :
}catch(Exception e){
e.printStackTrace();
}
Re: Having trouble loading variables from a file
It still gives me an error, and eclipse still gives me the same errors I quoted earlier.
Re: Having trouble loading variables from a file
You should simplify your problem to solve it. Just use a simple text file and a very simple Java program that reads in the text file into say a collection of Strings. If and when that works, move to the next step, perhaps parsing the information into numbers if that is necessary, or marshalling it into objects. Continue adding functionality in a step-wise fashion until the problem is solved or until you reach a step that has an error that can't be solved. Then post the code here.
Re: Having trouble loading variables from a file
Well, I made a new and simple program and I think I found the problem. The problem has something to do with the array. The array isn't updated even when the variable for "levelsizey" is. What I'm having trouble with is updating the array after the header file has been read. When I try moving the array to underneath where the Header functions are called, it gives me an error saying that the only valid modifier is "final", but when I change it to final it gives me an error saying something along the lines of "can not convert from %missing% to string" and tells me to change the final back to a String. Any suggestions?