How do I change my game map after player reaches a certain tile?

I’m making a 2D game where each level is determined by a config.json file. It contains the level name, and the name of a file called “level1.txt”, “level2.txt” etc. I have a buffered reader that reads the txt file, and prints out a tilemap based on the txt file.

I have a variable called currentLevelIndex that is an index which determines which level.txt to load.
Once the player reaches the exit tile, it should load the next level’s map.

How can I rerun the code that reads the config file? It’s in setup so I can’t figure out how to rerun the code without having to restart the entire program

Edit: The tilemap is stored in an arraylist, and in draw(), I have loop that draws out the tiles depending on what is stored in the arraylist

The bufferedreader I used updates the arraylist with the map, but that only works for level 1 as its in setup.

Edit 2: This is what I’m currently thinking:

  1. Make the bufferedreader thing a separate function, and not in setup()
  2. Call the function at the start of draw()
    However, this means the map will have to be read every single frame, and I’m not sure if thats bad or not

Hi @Marfmamow,

Yes you are exactly right :wink: :

  • Make a separate function so you can reuse it in other parts of the code
  • Doing an expensive operation in the draw() function should be avoided (I/O and file read can be).
    But you can call it once in the setup() function to load your first level then during the game when the player enter a new level, call the function again once and load that level.

Also you could load all levels at setup, store them in some kind of list (array, array list, map, whatever suits the data and needs) and in draw, just get next level data when needed…