AlmasB / FXGL

Java / JavaFX / Kotlin Game Library (Engine)
http://almasb.github.io/FXGL/
MIT License
4.42k stars 554 forks source link

[embedded mod]Add a callback function to embeddedLaunch? #1074

Open chengenzhao opened 3 years ago

chengenzhao commented 3 years ago

Please discuss the feature ideas on Gitter first. Then please describe the feature Add a callback function to embeddedLaunch method thus when fxgl game is fully launched, the callback function will be called Because currently the fxgl game launching is async so if we embed the fxgl pane into javafx pane, then this async process may cause concurrent conflicts If we could have a callback function to this method the conflicts will be easily avoided

Describe alternatives you've considered (optional) Currently we use EngineService and make a flag which isFirstInit = true then in the onGameReady method we need to judge if it isFirstInit if true then do the callback work and turn isFirstInit to false not very clean code and hard to maintain

AlmasB commented 3 years ago

There are a few key moments when the callback can occur.

The code that needs to be repeatedly called after each game start should go into an EngineService::onGameReady. I believe this is not your use case since you are using isFirstInit. The code that needs to be called once per application after the main loop started should go into an EngineService::onMainLoopStarting.

This leaves us with one use case that is currently not covered: the moment after FXGL.* calls can be made, but before the game is ready. I think it would be beneficial for this callback to be called after FXGL.* calls are valid since the above use cases are already covered with current API.

chengenzhao commented 3 years ago

Good Morning @AlmasB We use this feature simply because the FXGL application has to start with JavaFX program We have to start and cache it. It is async start so we have to disable JavaFX components and wait it fully started then enable those components. And when we enter the battle scene we will put FXGL pane into JavaFX container to display the game and remove it when the battle is over so if Add embeddedShutdown to decouple fxgl instance from javafx instance is solved then we probably dont need this callback We could start FXGL application when we need it and shut it down and restart again...