Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+23 votes

I was working on the load functionality in my team and I got it to load the saved maps, however, whenever I try to edit the loaded file further it reverts back to the map that I was working on before I loaded the file. This happens as soon as I click on a tile. How can I edit the canvas so that it remains as the one that I can keep editing?

asked in CSC305 Fall 2022 by (1 point)

2 Answers

+12 votes

This is an easy thing to have happen if you're not careful that you're only storing the map ONE place in your code.

For example, you may want to store the "one true current map" object as a private static reference inside your App class, with getCurrentMap() and setCurrentMap() helper methods.

Then:

  1. make sure to call the App class's setCurrentMap() method when you load the map from a file, and
  2. WHENEVER you are doing anything with the map (e.g. editing it, previewing it, etc), make sure that you are calling App.getCurrentMap(), rather than referring to an old map stored in an instance variable in a different class.

This is arguably another application of the DRY principle (sort of), albeit for data storage rather than about instruction codes. i.e. don't repeat yourself by storing multiple references to the terrain map in different classes. Instead, ONE class should be responsible for storing/maintaining the current terrain map object.

Something similar to the Singleton design pattern could be useful for your team to restrict access to the Terrain Map's constructor, and prevent your developers from creating more than one object. (However, the Singleton pattern may not work quite right because you need to be able to create a new object when you load from JSON... so a bit of modification to the pattern would be necessary.)

answered by (5.7k points)
+5

Thanks, professor, I was able to fix the bug.

+10 votes

It sounds like you are loading the file, however it sounds like you are not editing the file you loaded, instead editing an instance of the file, not the file itself. I would look into how your program references the file you are editing

answered by (1 point)
...