is the Container.sceneMain function a call back wh...
# korge
r
is the Container.sceneMain function a call back which is called every tick?
d
It is a
suspend fun
https://github.com/korlibs/korge/blob/bc4cfa3914518e8588f1146eec6ea9bf76ca1095/korge/src/commonMain/kotlin/com/soywiz/korge/scene/Scene.kt#L75-L82 It is executed once the scene is set once. There you can place a loop or something if required or just return. But if the scene is changed, the kotlinx.corotuines
Job
associated is cancelled and that suspending function is terminated
r
thanks. As someone who is new to Kotlin, JVM and Korge, its not been easy goings. 🙂 Here are some issues that I am struggling with; 1. Not sure how the scenes, input and stage work together. Can't find any example/sample which can help me here 2. where does sceneMain fits in the scene lifecycle 3. Intellij is marking the init in
override suspend fun init(injector: AsyncInjector): Unit = injector.run {
as deprecated. should i be using it? 4. I still didn't fully understand how the engine works, from the docs. I am used to the callback based engines having load(), update() and draw() functions. sceneInit seems like the load() callback. but where does the code which runs every tick goes in? Is it the addupdatable function?
d
Thanks for the good feedback!, we will try to improve the documentation to make stuff clearer. In the meantime let me give a small overview explanation about how does it work: There is a tree of views. It acts similar to the DOM in HTML. There are leaf views, and container-like views that can contain other views. Each view has transformations: position, rotation, scaling, alpha etc. Views can have components attached to them, they mostly react to events when they are added to the Stage. The stage is the Container View that is on the root. (The root node.) You can find a sample about scenes here: https://github.com/korlibs/korge-samples/tree/master/sample-scenes Regarding to the lifecycle methods of the scenes. The best place to read about them is the code itself here: https://github.com/korlibs/korge/blob/bc4cfa3914518e8588f1146eec6ea9bf76ca1095/korge/src/commonMain/kotlin/com/soywiz/korge/scene/Scene.kt#L15-L29 Please, check the lifecycle of each method to decide where to put your code. The init method is deprecated, you can use sceneInit (won’t display anything until ended) or sceneMain (will display stuff if suspended since its start) instead You can create a custom View creating a subclass of it, then override the render method. But that’s not the normal way of doing things. You normally just create containers and views and place them in the views tree. If you want to execute a code everyframe, you can use the:
addUpdater { ,,, }
method like shown here: https://github.com/korlibs/korge-samples/blob/1771b7ca7f4440e1a368ff4b441e97bf62e08b8d/sample-tilemap/src/commonMain/kotlin/main.kt#L31 To handle input, you can use an addUpdater, then use
views.input.keys
or
views.input.mouse
or use event-based callbacks:
mouse { up { … } down { … } }
keys { up(Key.LEFT) { … } }
Hope it helps! and thanks for your patience. We are planning to improve the documentation and to add tutorials during this and next weeks
r
Thank you! and sorry about making you type in so much! ^_^