0
votes

I know there is a lot of topic on that issue but for my case the only one I found here wasn't answered.

So i'm asking the same question:

Do I need to worry about setting my AssetManager static for a desktop only Game ?

Now just to summarize and clarify the Mobile Game static reference problem, lets say I also wanted to use a static AssetManager on a Mobile Game (because I find it more convenient). I know this will cause the Texture to be disposed on pause() but I can just reload them on resume() right ?

Lets say I have an animation for a player that use the Texture A (previously loaded into a static AssetManager). If the game is paused, the AssetManager reference will most likely be lost. If on the resume() method I recreate this AssetManager (MyGame.asset = new AssetManager()) and I reload the Texture A, what will happend to my animation ? Will I need to reload it again also ?

I found this phrase in the Texture Javadoc : Managed textures get reloaded automatically. I assume this is only valable for non static AssetManager am I right ?

The last questions are only here to summarize the use of a static AssetManager on Mobile Application.

1
static variable is not good in object oriented design, You should avoid static reference of your assets because the life cycle of the statics might not be the same as the life cycle the context the resources are created in.Abhishek Aryan
Ye, what Abhishek saidLucas B

1 Answers

1
votes

As Abhishek Aryan correctly pointed out: using static variables for your resources is a serious indication of a fundamental design flaw. You should worry about that regardless the platform you are running your application on, so also on desktop. Depending on the rest of your design you might or might not run into issues.

Usually with these kind of design flaws, it seems to be working just fine for you at first. But later on you will run into issues. Unfortunately by that time you are already so far progressed in your development that you can't easily correct the fundamental issue and need to hack around it instead.

Note btw that it technically is perfectly possible to use static resources on any platform as long as you properly manage them (which is harder than just not using them). For example, libgdx itself is build around the Gdx static members.

To answer your other questions:

I know this will cause the Texture to be disposed on pause() but I can just reload them on resume() right ?

That is incorrect. Textures will never be disposed in pause, nor do you need to reload them in resume.

If the game is paused, the AssetManager reference will most likely be lost.

That is incorrect. I'm not sure why you would think that, but that's not the case.

Will I need to reload it again also ?

If you decide to dispose your old texture (which you would have to do yourself, it is not done automatically for you) then any Animation that uses that texture will be invalid. So, indeed if you want your animation to use a different texture then you will have to either update your animation or recreate it.

I assume this is only valable for non static AssetManager am I right ?

No, managed textures don't care whether you are using static resources. They are are always reloaded when the context is loss. Note that it is very unlikely that you will ever run into this situation, so you don't have to care about this.

You might also want to read this post i wrote a while ago on this topic.