godot-kotlin-jvm
godot-kotlin-jvm copied to clipboard
Automatically running game with IDEA and Godot debuggers attached after a Gradle build
Writing this as documentation for how I got this working.
The goal
- Click run/debug in IDEA (or press Shift+F9/F10).
- Gradle builds the Kotlin project.
- Game is automatically started after the build finishes.
- Game process can be debugged both from IDEA and Godot editor with no additional user action.
The how
- Add the following to
build.gradle.kts. This creates a Gradle task which will run the game, telling it to attach to the Godot editor for debugging.
tasks.register<Exec>("runGodotGame") {
// Avoid running the game before rebuilding the project.
mustRunAfter("build")
// This can be an arbitrary path.
setWorkingDir(projectDir.absolutePath)
commandLine(
"/path/to/godot.editor.jvm",
// Attach to Godot editor (requires enabling Debug -> Keep Debug Server Open).
"--remote-debug", "tcp://127.0.0.1:6007",
// Start main scene of the project instead of the editor.
"--path", projectDir.absolutePath,
)
}
- Change
godot_kotlin_configuration.jsonto include the following line (thejvm_argsproperty should already exist). This makes the game attach to IDEA for debugging (note theserver=n).
{
// ...
"jvm_args": "-agentlib:jdwp=transport=dt_socket,server=n,address=localhost:5005,suspend=y",
// ...
}
-
In the Godot editor menu, enable option
Debug -> Keep Debug Server Open. This makes the Godot editor listen for our game process wanting to start remote debugging. -
In IDEA, go to
Run -> Edit Configurations, then create aRemote JVM Debugtask. You can name itlisten. Changedebugger modetoListen to remote JVM, and enableAuto restart. Keep the host aslocalhostand port as5005(as this is what we entered in step 2). -
In IDEA, go to
Run -> Edit Configurations, then create aGradletask. You can name itbuild and run game. Set theRun -> Tasks and argumentstext field to:build :runGodotGame. -
Finally, in IDEA, run the
Remote JVM Debuglistentask in debug mode, and then theGradlebuild and run gametask in any mode. Thelistentask should continue running after the game is closed, allowing another game process to connect.At this point the game should've started, and you should be able to debug the game using both IDEA's debugger and Godot's remote debugger.
-
(Optional) Remove
--jvm-debug-port=5005 --wait-for-debugger=falseoptions fromeditor/run/main_run_argsin Godot project settings. This will allow running the game from the Godot editor.
What would make this easier
-
Having to change the
jvm_argsingodot_kotlin_configuration.jsoncreates a permanent change to the project configuration. Having an argument like--jvm-debug-portwhich would make the JVM debug agent connect to a running server instead of starting one would allow putting everything in the Gradle build configuration. -
Being able to run the game immediately after a Gradle build triggered in Godot would be neat. This can currently be achieved by making the
runGodotGametask always run afterbuild, but that seems undesirable. -
I don't know if it's specific to me, but at least on my system stopping the Gradle task in IDEA doesn't close the game. This could be due to the game process detaching from the exec task's shell.