Loading JSON file not reflecting changes

Viewed 80

I have a JSON file I use to store dialogue for a game. However, the changes I make to this JSON file are sometimes not reflected in my game even after hard reloads (I've been doing ctrl+shift+r or shift+F5 for this). I have made sure the changes to the JSON file are saved.

I have this.load.json('dialogue', 'assets/dialogue.json'); in preload(), and this.dialogue = this.cache.json.get('dialogue'); in create().

When I try copy+pasting the contents to a new different file (e.g. dialogue-2.json), and update my this.load.json() to reflect the new file name, the changes do get loaded.

1 Answers

The problem is that phaser doesn't automatic start the loading files, except in the preload function.

If you want to load files in other functions you need to call the start method of the LoadPlugin. Here the link to the official documentation.

The code should look something like this:

function create(){
    // some code here ... 
    this.load.json(/* key and file...*/);
    // start the loading of the enqueued files
    this.load.start();
    // some code here...
}

..., than the files should get loaded.

Update (copied form comments):

to check (and as a tempoary quickfix) if your problem, is a caching issue from the Webserver or browser or ..., you can load the file with a "dynamic" Url. Just a load the file with a questionmark and random number added to the url, so that each call to the json uses aunique Url.

For Example like this:
this.load.json('dialogue', 'assets/dialogue.json?' + Math.random());

If this solves the problem, there is a caching problem. It could be in the browser, or simply caused by serviceworker or on the webserver.

Related