An addon for Godot that adds basic scriptable source generation. Inspired by C# source generators.
All you have to do is extend ScriptGenerator and Click Run Source Generation in Project -> Tools.
# minimal_generator.gd
extends ScriptGenerator
func get_source_data() -> Array:
return ["1", "2"]
func get_file_name(source_data: Variant) -> String:
return "example_" + source_data
func generate_source(source_data: Variant) -> String:
return """extends RefCounted
class_name Example{name}
func test() -> String:
return \"Hello from {name}\"
""".format({ "name": source_data })This generator will generate the following files:
# _generated_/example_1_gen.gd
extends RefCounted
class_name Example1
func test() -> String:
return "Hello from 1"# _generated_/example_2_gen.gd
extends RefCounted
class_name Example2
func test() -> String:
return "Hello from 2"Requires Godot 4.5 or higher
- Copy
gdscript_source_generationfrom the addons folder in this repo to your project's addons folder. - Open Project Settings -> Plugins then enable the plugin.
Read my post for why I made this: https://jacobcoughenour.com/posts/gdscript-fake-generics/
No.
Yes. Both the generate and clean commands are available as scripts. In the same directory that your project.godot file is in, you can run them like this:
godot --script addons/gdscript_source_generation/generate.gd
godot --script addons/gdscript_source_generation/clean.gd
The
generatescript also cleans up the generated files so you don't have to always runclean
That is up to you. You can chose to ignore the _generated_ folders but . To get around any automated build issues, add godot --script addons/gdscript_source_generation/generate.gd to your build script.
This makes it easier for me to clean up all the generated files before generating new ones. I had it ending the files with .gen.gd but that broke gdUnit4 when I was generating unit tests.
Yes, If you override the destination_directory_path() method in your generator. It will still append _generated_ to whatever path is returned.
As long as I'm actually using it for my own projects.