korlibs / korge

KorGE Game Engine. Multiplatform Kotlin Game Engine
https://korge.org/
Other
2.55k stars 125 forks source link

scene changeTo is not working within fixedUpdater #1156

Open MYKIM95 opened 1 year ago

MYKIM95 commented 1 year ago

Hello, I am trying to change scene when player's health goes to 0. I wrote my code as below to check player's health every 1 seconds, and if health goes to under 0, the scene will change to the end scene.

addFixedUpdater(time = 1.seconds) {
    if(health <= 0) {
        launch {
            sceneContainer.changeTo({ SceneEnd() })
        }
    }
}

I got error log like below, and the scene does not changed. I/System.out: Changing scene to... class com.lok.dev.korgegame.scene.SceneEnd (Kotlin reflection is not available) ... com.lok.dev.korgegame.scene.SceneEnd@241a353 W/System.err: kotlinx.coroutines.JobCancellationException: Job was cancelled; job=JobImpl{Cancelled}@d8e0189

If I use changeTo function without FixedUpdater and launch, it works well.

gmitch215 commented 3 months ago

+1. @soywiz wondering what progress you've made on this issue?

soywiz commented 3 months ago

Does it reproduce with KorGE 6.0.0-alpha9?

El mar, 9 jul 2024 a las 18:06, Gregory Mitchell @.***>) escribió:

+1. @soywiz https://github.com/soywiz wondering what progress you've made on this issue?

— Reply to this email directly, view it on GitHub https://github.com/korlibs/korge/issues/1156#issuecomment-2218098302, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAELLYHYJGUOC4ADW7H5RCTZLQDA3AVCNFSM6AAAAABKTFVMPGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMJYGA4TQMZQGI . You are receiving this because you were mentioned.Message ID: @.***>

gmitch215 commented 3 months ago

Does it reproduce with KorGE 6.0.0-alpha9? El mar, 9 jul 2024 a las 18:06, Gregory Mitchell @.>) escribió: +1. @soywiz https://github.com/soywiz wondering what progress you've made on this issue? — Reply to this email directly, view it on GitHub <#1156 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAELLYHYJGUOC4ADW7H5RCTZLQDA3AVCNFSM6AAAAABKTFVMPGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMJYGA4TQMZQGI . You are receiving this because you were mentioned.Message ID: @.>

Yes.

kotlinx.coroutines.JobCancellationException: Job was cancelled; job=JobImpl{Cancelled}@2347a220
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: index: 1, size: 1
    at korlibs.datastructure.FastArrayList.throwIndexOtOfBounds(FastArrayList.concurrent.kt:212)
    at korlibs.datastructure.FastArrayList.get(FastArrayList.concurrent.kt:223)
    at korlibs.korge.view.Container.getChildAt(Container.kt:167)
    at korlibs.korge.view.Container.get(Container.kt:273)
    at korlibs.korge.scene.TransitionView.getNext(Transition.kt:31)
    at korlibs.korge.scene.TransitionView.startNewTransition(Transition.kt:39)
    at korlibs.korge.scene.SceneContainer._changeTo-dWUq8MI(SceneContainer.kt:282)
    at korlibs.korge.scene.SceneContainer._changeTo-1Y68eR8(SceneContainer.kt:522)
    at korlibs.korge.scene.SceneContainer.pushTo-1Y68eR8(SceneContainer.kt:185)
    at xyz.calcugames.combinatory.ui.scene.ControllerKt.levelSelect(controller.kt:60)
plugins {
    id("com.soywiz.korge") version "6.0.0-alpha9"
}
gmitch215 commented 3 months ago

Still Persistent in 6.0.0-beta1.

gmitch215 commented 3 months ago

Workaround:

Replace

injector.mapSingleton { MyScene() }

with

injector.mapPrototype { MyScene() }

@soywiz Does this help with identifying the issue?

soywiz commented 2 months ago

Oh, I see. Scenes must use mapPrototype and not mapSingleton. By using mapSingleton the same instance would be reused, and it is not designed to work like that. What we can do is to check that the instance is different when changing the scene or otherwise do nothing or report a warning or something about this.