How can I handle Assetmanager fallbacks cleanly?

Hi,

basically I hae this code, wich works but is ugly as fuck. What I want to do is, to have a fallback texture for all non locateable textures (and later a similar system for sounds,ect) As i prefer to see a ugly model/ hear a glitch than have the game crashing.

This should also work for dependent ressources, eg a texture used by a model.
The assetDependecyNotFound seem to only be a error hook, as an exception is thrown afterwards anyway.

my current approach is this:

manager.addAssetEventListener(new AssetEventListener() {
 
            @Override
            public void assetRequested(AssetKey key) {
                AssetInfo location = manager.locateAsset(key);
                if (location == null) {
                    LOGGER.warn("Asset not found " + key);
                    if (key instanceof TextureKey) {
                        TextureKey dummy = new TextureKey("materials/noconvert/error.jpg");
                        if (dummy.equals(key)) {
                            return;
                        }
 
                        try {
                            List<Field> fields = ReflectionUtils.getAllFields(key.getClass(), true);
                            for (Field f : fields) {
                                if (f.getName().equals(ASSETKEY_NAME_VAR)) {
                                    f.set(key, f.get(dummy));
                                }
                                if (f.getName().equals(ASSETKEY_EXTENSION_VAR)) {
                                    f.set(key, f.get(dummy));
                                }
                            }
                        } catch (IllegalArgumentException e) {
                            e.printStackTrace();
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
 
            @Override
            public void assetLoaded(AssetKey key) {
 
            }
 
            @Override
            public void assetDependencyNotFound(AssetKey parentKey, AssetKey dependentAssetKey) {
                System.out.println("Not found " + parentKey + " " + dependentAssetKey);
            }
        });

Hm… Normally the loader doesn’t crash when a texture is missing but just returns a model without texture…? I use this in the SDK to inform the user about textures that couldn’t be loaded but the model normally still loads. Unless you use the blender loader in your game, that one always had some funny edge cases where it crashes instead of properly returning a model with all data it can get as its supposed to.

Even with j3o? Cause i could bet i had some strange problems there. Anyway the error texture I want to use is highly visible so I would prefer it to the default material.

Btw. just started getting bullet compile to work again, to start working on that meshshape bug. Build works out of the box, great work.

If you get no crash and get the callback for the missing texture you can just apply it afterwards, no?

I think @Momoko_Fan made something like that for textures.

Yes … at least in 3.1, loading a J3O with missing textures will result in a placeholder “Missing Texture” being displayed. You can just replace it in the jme3-core.jar if you don’t like it and want it to be more prominent or whatever.