godot-kotlin-jvm icon indicating copy to clipboard operation
godot-kotlin-jvm copied to clipboard

Automatically running game with IDEA and Godot debuggers attached after a Gradle build

Open opl- opened this issue 10 months ago • 0 comments

Writing this as documentation for how I got this working.

The goal

  1. Click run/debug in IDEA (or press Shift+F9/F10).
  2. Gradle builds the Kotlin project.
  3. Game is automatically started after the build finishes.
  4. Game process can be debugged both from IDEA and Godot editor with no additional user action.

The how

  1. 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,
	)
}
  1. Change godot_kotlin_configuration.json to include the following line (the jvm_args property should already exist). This makes the game attach to IDEA for debugging (note the server=n).
{
	// ...
	"jvm_args": "-agentlib:jdwp=transport=dt_socket,server=n,address=localhost:5005,suspend=y",
	// ...
}
  1. 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.

  2. In IDEA, go to Run -> Edit Configurations, then create a Remote JVM Debug task. You can name it listen. Change debugger mode to Listen to remote JVM, and enable Auto restart. Keep the host as localhost and port as 5005 (as this is what we entered in step 2).

  3. In IDEA, go to Run -> Edit Configurations, then create a Gradle task. You can name it build and run game. Set the Run -> Tasks and arguments text field to :build :runGodotGame.

  4. Finally, in IDEA, run the Remote JVM Debug listen task in debug mode, and then the Gradle build and run game task in any mode. The listen task 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.

  5. (Optional) Remove --jvm-debug-port=5005 --wait-for-debugger=false options from editor/run/main_run_args in Godot project settings. This will allow running the game from the Godot editor.

What would make this easier

  1. Having to change the jvm_args in godot_kotlin_configuration.json creates a permanent change to the project configuration. Having an argument like --jvm-debug-port which would make the JVM debug agent connect to a running server instead of starting one would allow putting everything in the Gradle build configuration.

  2. 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 runGodotGame task always run after build, but that seems undesirable.

  3. 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.

opl- avatar Jan 26 '25 14:01 opl-