Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (807)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (872)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  libgdx - use Gdx.files.internal without android module?  (Read 1264 times)
0 Members and 1 Guest are viewing this topic.
Offline ral0r2
« Posted 2020-04-11 15:41:07 »

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:

1  
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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
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

Offline LiquidNitrogen
« Reply #1 - Posted 2020-04-12 04:39:56 »

it will automatically look in core/assets/, so you can just use Gdx.files.internal("maps/map1.tmx") to find your file
Offline ral0r2
« Reply #2 - Posted 2020-04-12 20:13:38 »

Unfortunately I still get this exception:

1  
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 Sad
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ral0r2
« Reply #3 - Posted 2020-04-13 15:11:47 »

I could fix it. Apparently I was missing
1  
assetManager.finishLoading();


Thanks for the help!
Offline FabulousFell

Junior Devvie


Medals: 6



« Reply #4 - Posted 2020-04-13 17:55:30 »

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?
Offline ral0r2
« Reply #5 - Posted 2020-04-14 07:36:02 »

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).

https://github.com/libgdx/libgdx/wiki/Managing-your-assets

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
1  
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.
Offline LiquidNitrogen
« Reply #6 - Posted 2020-04-16 00:13:50 »

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
Offline ral0r2
« Reply #7 - Posted 2020-04-16 14:55:24 »

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?
Offline VaTTeRGeR
« Reply #8 - Posted 2020-04-16 15:28:18 »

Quote
Why do I have to call finishLoading() then in my case?
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?
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!

1  
2  
3  
4  
5  
6  
7  
8  
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");
}
Offline ral0r2
« Reply #9 - Posted 2020-04-16 19:09:15 »

Quote
Why do I have to call finishLoading() then in my case?
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?
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!

1  
2  
3  
4  
5  
6  
7  
8  
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");
}


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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline FabulousFell

Junior Devvie


Medals: 6



« Reply #10 - Posted 2020-04-17 20:29:09 »

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.
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (843 views)
2019-09-04 15:33:17

hadezbladez (5778 views)
2018-11-16 13:46:03

hadezbladez (2597 views)
2018-11-16 13:41:33

hadezbladez (6195 views)
2018-11-16 13:35:35

hadezbladez (1494 views)
2018-11-16 13:32:03

EgonOlsen (4729 views)
2018-06-10 19:43:48

EgonOlsen (5779 views)
2018-06-10 19:43:44

EgonOlsen (3270 views)
2018-06-10 19:43:20

DesertCoockie (4171 views)
2018-05-13 18:23:11

nelsongames (5481 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!