libgdx - use Gdx.files.internal without android module?

Hey I’m currently playing around with the assetmanager of libgdx. I created a desktop project via the libgdx creator.
So I only have core and desktop in my project folder. Inside my core I got /assets/maps.
I now want to do something like:

Gdx.files.internal("core/maps/map1.tmx") 

But it always cant find anything. I think because it is looking for my asset folder in the android module? Any ideas how I can point it to an asset folder inside core?
I read about it online and something pointed me at adapting the build.gradle?

This is my fullcode:


private TmxMapLoader mapLoader;
private AssetManager assetManager;

    public TiledMapLoader(){
        assetManager  = new AssetManager();

        assetManager.setLoader(TiledMap.class, new TmxMapLoader(new InternalFileHandleResolver()));
    }

    public TiledMap initializeTiledMap(int level) {
        assetManager.load("maps/map_"+level+".tmx", TiledMap.class);
        return  assetManager.get("maps/map_"+level+".tmx");
    }

My folderstructure

it will automatically look in core/assets/, so you can just use Gdx.files.internal(“maps/map1.tmx”) to find your file

Unfortunately I still get this exception:

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Asset not loaded: maps/map_0.tmx

Edit: I adapted the code.

So Gdx.files.internal(“maps/map1.tmx”) works. But assetManager.load(“maps/map_”+level+".tmx", TiledMap.class); however doesnt work somehow :frowning:

I could fix it. Apparently I was missing

assetManager.finishLoading();

Thanks for the help!

How does using that function differ from using Gdx.files.internal(“core/maps/map1.tmx”) ? In the game I’m making with LibGDX I use -> Gdx.files.internal(“core/maps/map1.tmx”). I guess my question is, why do you have to use the finishLoading() method in the way you’re doing it, but not the way I do it? Does the asset manager just finish loading itself the way I do it?

To be honest I’m not entirely sure. As far as I understand the asset manager is a more structured way to manage your assets since it gives you a variety of options to interact with your assets like for instance:

  • Allows to transparently implement things like caches (see FileHandleResolver below)
  • Assets are reference counted. If two assets A and B both depend on another asset C, C won’t be disposed until A and B have been disposed. This also means that if you load an asset multiple times, it will actually be shared and only take up memory once!

Also the github describes manager.finishLoading() with the following quote:

[quote]This will block until all the assets that have been queued are actually done loading. Kinda defeats the purpose of asynchronous loading, but sometimes one might need it (e.g., loading the assets needed to display the loading screen itself).
[/quote]

I’m not really sure why I have to call it after loading my assets. I somewhere read that it is important to load the assets for the screen is called but I’m not really sure about it. In short I think

Gdx.files.internal("core/maps/map1.tmx") 

is a quicker more unstructerd way to access your assets how ever I doubt that it will have any disadvantages unless you are about to do a relatively big project.

The asset manager loads things in the background on a separate thread, so they wont actually be loaded until either you call finishLoading(), or until the process has completed loading them. that way you can display a progress bar while waiting for them to load, or you can allow the game to still run while not displaying the not-yet-loaded images

Why do I have to call finishLoading() then in my case? As far as I understand the process should complete loading them by itself?

[quote]Why do I have to call finishLoading() then in my case?
[/quote]
You request assets that haven’t been fully loaded from disk in the background (!) yet, hence why it works after calling the blocking finishLoading() method.

If you use the parallel/background loading of assetmanager (=in a different thread) you also need to handle the case that the asset isn’t available yet and throws an Exception to signal that that asset isn’t loaded yet.

[quote]As far as I understand the process should complete loading them by itself?
[/quote]
Yes after a certain delay, harddrives and even ssds when compared to RAM are slow af. You misunderstood what the assetmanager was doing internally i suppose. It doesn’t load the assets on the same thread!

public TiledMap initializeTiledMap(int level) {
    // You tell the AssetManager to load the level in it's background thread:
    assetManager.load("maps/map_"+level+".tmx", TiledMap.class);
    // Right after that you request the level asset:
    // This will most certainly give you an Exception since it takes some time (milliseconds)
    // to load the file in the background but entering the method is just a couple nanoseconds.
    return  assetManager.get("maps/map_"+level+".tmx");
}

You request assets that haven’t been fully loaded from disk in the background (!) yet, hence why it works after calling the blocking finishLoading() method.

If you use the parallel/background loading of assetmanager (=in a different thread) you also need to handle the case that the asset isn’t available yet and throws an Exception to signal that that asset isn’t loaded yet.

[quote]As far as I understand the process should complete loading them by itself?
[/quote]
Yes after a certain delay, harddrives and even ssds when compared to RAM are slow af. You misunderstood what the assetmanager was doing internally i suppose. It doesn’t load the assets on the same thread!

public TiledMap initializeTiledMap(int level) {
    // You tell the AssetManager to load the level in it's background thread:
    assetManager.load("maps/map_"+level+".tmx", TiledMap.class);
    // Right after that you request the level asset:
    // This will most certainly give you an Exception since it takes some time (milliseconds)
    // to load the file in the background but entering the method is just a couple nanoseconds.
    return  assetManager.get("maps/map_"+level+".tmx");
}

[/quote]
Ah damn! So I’m running into a race condition kinda there. Thank you very much @VaTTeRGeR makes sense now for me! So I either you finishLoading() or should ensure that my assets are loaded with enough time and safe thread handling. Thanks.

I tried to appreciate the last three posts in this thread, but I guess in this new account I don’t have that option. So, VaTTeRGeR and ral0r2 and LiquidNitrogen, if this login issue ever gets fixed so I can login via Google again, you got medals waiting lol.