utopia-rise / godot-kotlin-jvm

Godot Kotlin JVM Module
MIT License
585 stars 38 forks source link

Add the possibility to register scripts without constructor #488

Open CedNaru opened 1 year ago

CedNaru commented 1 year ago

In my project, I have many cases where I need to create classes inheriting a Godot base type (well, most of the time inheriting a Node). Except, the construction of the class itself requires parameters that are not Variant types (can be any other internal class). Those classes are not supposed to be instantiated from Godot itself, only from within Kotlin code directly.

Right now, my cheap trick to go around that is just to leave the 0 parameter constructor there and have a bunch of lateinit properties. But it's unnecessary, I'm never going to use the default 0 parameter constructor, and it also adds the script to the list of Node I can add directly in the editor, which obviously won't work.

The goal is that I want to be able to use dependency injection easily with my scripts. I know that Godot can accept scripts that can't be instantiated, but I don't know how flexible it is outside abstract scripts.

The requirement for such feature is to check if the Godot editor properly behave when dealing with scripts without constructor. In the best case, it shouldn't be possible to attach the script in the editor at all (manually, by custom type creation or GDScript). In the worst case, attaching that script would result in an error or undefined behaviour and writing such script in Kotlin should be an opt in.