Scene Container not initialized



  • I get an exception saying that the lateinit var sceneCntainer is never initialized for my resize/reload scene:

    class ResizingScene(returnTo: Scene): Scene() {
        val returnTo = returnTo
        var timeLeft = 250;
        override suspend fun Container.sceneInit() {
            stage!!.views.scaleAnchor = Anchor.TOP_LEFT;
            stage!!.views.scaleMode = ScaleMode.NO_SCALE;
            val resizing = text("Resizing", root.width / 15)
            val img = image(resourcesVfs["korge.png"].readBitmap(), 0.5, 0.5)
            img.xy(root.width / 2, root.height / 2)
            val size = if (root.width < root.height) root.width / 3.0 else root.height / 3.0
            img.setSize(size, size)
            resizing.format.size = (size / 3).toInt()
            resizing.xy((root.width / 2) - (resizing.width / 2), root.height / 10)
            var rsc = img.scale;
    
            this.addEventListener(ReshapeEvent::class) {
                img.xy(it.width / 2, it.height / 2)
                val size = if (it.width < it.height) it.width / 3.0 else it.height / 3.0
                img.setSize(size, size)
                rsc = img.scale;
                resizing.format.size = (size / 3).toInt()
                resizing.xy((it.width / 2) - (resizing.width / 2), it.height / 10)
                timeLeft = 250;
            }
            launchImmediately(EmptyCoroutineContext) {
                while(true) {
                    img.tween(img::scale[rsc - (0.3 * rsc)], time = 0.3.seconds, easing = Easing.EASE_IN_BACK)
                    img.tween(img::scale[rsc + (0.3 * rsc)], time = 0.3.seconds, easing = Easing.EASE_IN_BACK)
                }
            }
            launch(EmptyCoroutineContext) {
                while (timeLeft > 0) {
                    timeLeft--
                    delay(1);
                }
                sceneContainer.changeTo<Scene>(returnTo)
            }
        }
    
    }
    


  • The lifecycle of scenes are handled by a SceneContainer that is a view.
    The scene container initializes the Scene stuff:
    https://korlibs.soywiz.com/korge/scene/#SceneContainer

    You can call externally to:
    mySceneContainer.pushTo<ResizingScene>()
    then to return to the previous scene:
    sceneContainer.back()



  • @soywiz whats actually going on is resizing nine patches. I want to add a delay because it’s really slow to rerender them and I figured I’d just reopen the scene after resizing. My problem is that I can’t ever get to an initialized scene container from the corutine. I can’t even use

    sceneContainer::isInititialized // or something like that
    

    But why can’t I get an instance from the corutine?



  • You should't use EmptyCoroutineContext in any case when launching stuff since there is relevant stuff in the context. You are in a suspend function so you can use launcImmediately directly, and you can get the coroutineContext too



  • Regarding to nine patch they are here (there are two implementations, one used for swf and the other for user code):

    https://github.com/korlibs/korge/blob/master/korge/src/commonMain/kotlin/com/soywiz/korge/view/NinePatch.kt

    https://github.com/korlibs/korge/blob/master/korge/src/commonMain/kotlin/com/soywiz/korge/view/NinePatchEx.kt

    If you have performance problems, maybe it is possible to do some optimizations. I usually use the profiler for chrome tools to get an idea of whats going on