NokFrt

How to find out that a file failed to load

Recommended Posts

Is there a way how to find out that a file failed to load? There are "complete", "filecomplete", "fileprogress", … events but non of them tells me that the loading failed.

Also I found out that loader doesn't emit "complete" event if any of files failed to load and if this happens in scene.preload(), the scene.create() and scene.update() won't be called.

Share this post


Link to post
Share on other sites

I'm using this.load.JSON and then trying to detect if the file fails to load. If it is a "404" then I want to redirect the user to the loading page and log an error (etc). Whatever I find for this.load.JSON should probably also work with whatever filetype it is that you're using.

There isn't a namespace or class in the documentation that directly matches this.load.JSON or Phaser.load.JSON or Phaser.Load so this is where I've checked:

  • Namespace: Phaser.Loader is (I think) information about internal file handling or something similar. 
     
  • Namespace: Phaser.Loader.FileTypes has entry for JSONFileConfig but nothing on .load for types of files. I think this and the previous one must both be used by this.load.JSON.
     
  • Class: Phaser.Loader.File says for its load() method that "You shouldn't normally call this method directly, it's meant to be invoked by the Loader" so this is probably also used by this.load.JSON. It does also have an "onError" method.
     
  • Class: Phaser.Loader.FileTypes.JSONFile sounds like the perfect place to look; but it's class constructor doesn't match. It says "new JSONFile(loader, key [, url] [, xhrSettings] [, dataKey])" whereas I'm looking for .load(KEY, URL), and, it's .load() method also says "You shouldn't normally call this method directly, it's meant to be invoked by the Loader".
     
  • Class: Phaser.Loader.LoaderPlugin sounded like a loader for plugins (i.e., for adding plugins to Phaser3). And it was last in the Phaser.Loader classes, so I checked it last. But its opening description talks about using this.load (yay). It doesn't have a method called .load() unfortunately, but, it does have one called .json() which matches the code from the Examples which I used to load .JSON files:

So, that last one was the correct place to look.

Class: Phaser.Loader.LoaderPlugin.json(key [, url] [, dataKey] [, xhrSettings]) contains the documention for this.load.JSON. I was hoping there would be parameters along the lines of "errorCallback" which would be passed the HTML status code (i.e., HTTP 404 or HTTP 500). That class also has events like "completeEvent" and "loadErrorEvent".

I already use the "completeEvent"

config.t.load.once('complete', function (){gl_3_loadlLevel_onJSON();}, this);
config.t.load.json(pLevel+'levelData', 	"maps/"+pLevel+".json");	
config.t.load.start();

In the documentation, it looks like the "completeEvent" matches the 'complete', and therefore, that "loadErrorEvent" could be called 'loadError'. So I tried adding this.load.once('loadError'), but it doesn't fire when the wanted file causes an error (i.e. 404).

function loadLevel(pLevel){
	if(config.t.cache.json.has(pLevel+'levelData')){	//File already loaded into cache
		gl_3_loadlLevel_onJSON();
	}else{
		config.t.load.once('complete',  function (){gl_3_loadlLevel_onJSON();}, this);
		config.t.load.once('loadError', function (){gl_3_loadFileFail();     }, this);
		config.t.load.json(pLevel+'levelData', 	"maps/"+pLevel+".json");	
		config.t.load.start();
	};
};

function gl_3_loadFileFail(pFile){
	alert("uh oh");
};

function gl_3_loadlLevel_onJSON(){
    //Once a new JSON file is successfully loaded, this function is called
};

I'm still fairly sure that the answer is in Class: Phaser.Loader.LoaderPlugin somewhere (https://photonstorm.github.io/phaser3-docs/Phaser.Loader.LoaderPlugin.html#event:completeEvent__anchor ), it has the code for the .once() method, which says "Add a one-time listener for a given event" - sounds perfect, if you want to catch a possible onError type event for a file load. But, it doesn't list what the names of valid events are. "complete" works but "loadError", "loaderror", "loadErrorEvent" and "loaderrorevent" don't work.

I'll let you know if I get it working!

I wish "Phaser.Load" appeared in the documentation Classes menu, it would have made this search somewhat shorter!

Share this post


Link to post
Share on other sites

I've tried "onLoad" and other possible names for the "failed to load file" event, but still no luck. The documentation says that errorLoad "is fired when the a file errors during load"; maybe because it says during load, that this event only fires once a load has started, and therefore, won't be invoked in the case of a 404, where the file never starts? (Although a GET HTTP TCP session clearly starts, because the webserver returns the 404 status code to the browser, so I think it is unlikely that the errorLoad wouldn't fire).

Share this post


Link to post
Share on other sites

In one of the examples I saw a different technique for getting hooks to the file load events; https://labs.phaser.io/edit.html?src=src/loader/loader events/file complete event.js had:

this.load.image('taikodrummaster', 'assets/pics/taikodrummaster.jpg').on('filecomplete', addImage, this);

So I've tried:

config.t.load.json(pLevelID+'levelData',     "maps/"+pLevelID+".json").on('onError', function(){alert("hi")}, this);
config.t.load.start();

But the alert doesn't show - I've tried event names of "loadError", "onError"

Share this post


Link to post
Share on other sites

Got it! To catch when a JSON file doesn't load (presumably this will work for all filetypes):

function loadLevel(pLevelID){
	if(config.t.cache.json.has(pLevelID+'levelData')){	//File already loaded into cache
		gl_3_loadlLevel_onJSON();
	}else{
		config.t.load.once('complete',  function (){gl_3_loadlLevel_onJSON();}, this);
		config.t.load.json(pLevelID+'levelData', 	"maps/"+pLevelID+".json").on('loaderror', gl_3_loadFileFail, this);
		config.t.load.start();
	}
}

function gl_3_loadFileFail(){
	loadLevel('home');
};

So, the "loaderror" event works for .on but it is an all-lowercase name (the documentation has "loadError" with a capital E. Although doing .once("loaderror",...) didn't work (I tried previously).

Here's what could be improved in the documentation (for those who are listening, and have time...)

  1. Make it so that you can find "this.load.json" without having to search through (in detail) lots of documentation files, by adding "Phaser.load" to the menu, even if it merely redirects the browser to https://photonstorm.github.io/phaser3-docs/Phaser.Loader.LoaderPlugin.html .
     
  2. On that file, for the different occasions where it says "Add a listener for a given event" (for example in the entries for .on and .once), have a place where it lists all the possible valid event names (with correct capitalisation).
     
  3. In the "Events" section of that page, where it lists events like "loadErrorEvent", state what methods will work to call those events. For example, to call "completeEvent" you can use "this.load.once('complete')" (or other methods, probably), but for the "loadError" event list only the .on('loaderror',...) (plus whatever other methods work).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.