Quillraven / SimpleKtxGame

The LibGDX simple game with Kotlin and LibKTX using an assetmanager, pool and viewport
74 stars 14 forks source link

Calling dispose() in LoadingScreen #9

Closed maxlund closed 5 years ago

maxlund commented 5 years ago

Hey! Thanks for the helpful introduction guide to libktx!

I'm wondering about the calls to dispose() in LoadingScreen/MainMenuScreen, they are not doing anything since they are just override fun dispose() = Unit in KtxScreen.

and in MainMenuScreen before it got renamed here:

        if (Gdx.input.isTouched) {
            game.addScreen(GameScreen(game))
            game.setScreen<GameScreen>()
            game.removeScreen<MainMenuScreen>()
            dispose()
        }

and in LoadingScreen after it got renamed:

        if (Gdx.input.isTouched && assets.isFinished) {
            game.removeScreen<LoadingScreen>()
            dispose()
            game.setScreen<GameScreen>()
}
Quillraven commented 5 years ago

Hi,

I could have mentioned that in a comment, you are right :) dispose() in this case is actually doing nothing because all disposable things are created in the Main game class and get disposed when the game shuts down.

The lines were added mainly as a safety in case LoadingScreen or MainScreen are creating resources that need to be disposed. Since dispose does not get called automatically when you switch screens, I just wanted to make sure to call them for the "One time" screens that are only used once and get removed after we switch away from them.

But again, in the example you can simply remove the call. It does not make any difference.

Regards Simon

maxlund commented 5 years ago

Ah okay, thanks for the quick answer and for clarifying that "dispose does not get called automatically when you switch screens".. good to know i should override the method and make sure to dispose of any resources before i switch screens! :)

Quillraven commented 5 years ago

Personally I never needed the screen dispose method because nowadays I just use the libktx context that I create and dispose in the Game class. The context contains the assetmanager, spritebatch and other disposables that then get automatically disposed when calling context.dispose().

I think especially for small games it makes it very easy and convenient.

For big games I can imagine this is not the best practice as you will need to keep all your resources in memory all the time which might not be good.

But for me it was always small pixelated games so keeping those ~50MB resources in memory is not an issue at all and therefore there was no need for a releasing/reallocating of resources during the lifetime of the game ;)

If you aim for a small game then I would recommend that approach first as it makes it way easier.

maxlund commented 5 years ago

Sounds good! I didn't look into using injection provided by libktx yet, I'm going through java based documentation and tutorials (like your own on YT), but writing everything in kotlin.. Hopefully can get into using libktx more when I get comfortable with the fundamentals :)

Quillraven commented 5 years ago

I will close this issue then, ok?

for further questions feel free to contact me here or via quillraven@gmail.com