I am currently developing a simple 2D game that is like Minecraft in Eclipse Java. One of the features I am currently trying to debug is saving a generated 2D noise map into a source folder separate from the root folder called resources and then using that same file that was saved to generate the map that will be displayed when the game runs. The issue that I am having is that my program does not display the recently generated map that was saved, it will display the previous map (from other test runs).
The different times the program was able to display the recently generated map is when:
- I run the program, close it, hit refresh in Eclipse, then run the program again.
- Find the project file, go into the bin folder, and delete the previous generated map that Java compiled.
- Comment out the code that displays the map, run the program with the code that generates the map, then run the program again with the code that displays the map.
- Open the file that contains the map data in Eclipse, have it update, then run the program.
- Run the program many times until it decides to update.
I can confirm that the program successfully generates the map, and saves it with each run so I do not think there are any bugs in that sense. My assumption is that the program can save the map data to the resources source folder but for some reason is not available during run time? Which is weird because the program looks in the same place that the map data is stored to display to the player. If anyone can point me in the right direction or at least notice if I am doing something wrong it will be greatly appreciated.
Below are screenshots and code:
[Structure of Files][1]
Main construction where objects are called:
public GamePanel() {
this.setPreferredSize(new Dimension(screen_width,screen_height));
this.setBackground(Color.gray);
this.setDoubleBuffered(true);
key_handler = new KeyEvents();
this.addKeyListener(key_handler);
this.setFocusable(true);
player = new Player(this,key_handler);
//generate map and save map data
map_generator = new TileMap(this);
//load saved map data to be displayed
tile_manager = new TileManager(this);
}
Function of the map_generator object that saves map data:
void saveMap(String map) {
Path resource_path = Paths.get("resources","map_files");
String map_path = resource_path.toFile().getAbsolutePath() + "\\" + map;
try {
OutputStream map_stream = new FileOutputStream(map_path);
for (int r=0; r<map_height; r++) {
String map_row = "";
for (int c=0; c<map_width; c++) {
map_row = map_row + map_array[r][c] + " ";
}
byte[] bytes = map_row.getBytes();
map_stream.write(bytes);
if (r!=map_height-1)
map_stream.write(10);
}
map_stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Function of the tile_manager object that loads map data to be displayed to player
public void loadMap(String map) {
try {
InputStream map_stream = getClass().getResourceAsStream(map);
BufferedReader map_reader = new BufferedReader
(new InputStreamReader(map_stream));
int col = 0;
int row = 0;
while (col < game_panel.col_size && row < game_panel.row_size) {
String map_line = map_reader.readLine();
while (col < game_panel.col_size) {
String numbers[] = map_line.split(" ");
int num = Integer.parseInt(numbers[col]);
map_file[col][row] = num;
col++;
}
if (col == game_panel.col_size) {
col = 0;
row++;
}
}
map_reader.close();
} catch(IOException e) {
e.printStackTrace();
}
}