godotgdscriptgodot3

How to change a scene's exported variable via tool mode?


I have a scene as such:

enter image description here

where the main scene has a show_title variable and before I export the game I run the following plugin:

tool
extends EditorExportPlugin
class_name CustomExportPlugin


func _export_begin(features, is_debug, path, flags):
    # do some checks before exporting
    ...

How do I set the show_title=true within the Main.tscn every time before exporting?

Note: I am using godot version 3.5


Solution

  • Modifying an scene from a tool script

    Yes, this modifies the project. And no, EditorExportPlugin IN GODOT 3, does not make this any easier.

    IN GODOT 4 you can override _begin_customize_scenes to return true, and override _customize_scene to return the modified PackedScene. This way you don't have to save it, and thus the project is not modified.


    What I would do

    In the script on the root of main.tscn:

    func _ready() -> void:
        show_title = true
    

    If you need this set before _ready, you can do it in _init instead.

    If that script is not a tool script, that won't happen in the editor. If it is, then you can check if the code is running in the editor with Engine.editor_hint (Engine.is_editor_hint() in Godot 4).

    🤷