How open TXT files to edit in script editor

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By RikKargones

Hi.

My task is:
Open simple txt files in script editor from gdscript code safely.

I know, script editor can open txt files from editor’s user interface, but how I can do this trick from code? I find some solution, but what way isn’t stable for Engine.

So, my code here:

tool
extends Node2D

export (String, FILE, "*.txt")  var cutscene_file = "" setget open_file

export (bool)  var to_edit = false setget back_to_write

func open_file(new_path):
    if (cutscene_file != new_path):
        cutscene_file = new_path

func back_to_write(go):
    if Engine.editor_hint && go:
        var file = load(cutscene_file)
        if (file != null):
            EditorPlugin.new().get_editor_interface().edit_resource(file)
            # ^ This row make these problems
        file = null

If code runs successfully, I see this errors in output:

core/object.cpp:2012 - Object [Object:34316] was freed or unreferenced while a signal is being emitted from it. Try connecting to the signal using 'CONNECT_DEFERRED' flag, or use queue_free() to free the object (if this object is a Node) to avoid this error and potential crashes.

core/object.cpp:2012 - Object [Object:34315] was freed or unreferenced while a signal is being emitted from it. Try connecting to the signal using 'CONNECT_DEFERRED' flag, or use queue_free() to free the object (if this object is a Node) to avoid this error and potential crashes.

In other rare cases, Engine crashes.

How can I get an editor interface in code? I didn’t find anything in code reference, what not includes “EnginePartThing.GetOtherThingThatYouNeed” to do.

:bust_in_silhouette: Reply From: exuin

This plugin can edit text files in Godot, so maybe you can check out the code there?

Wow!) Really nice thing!)

I made some simple plugin myself at what moment, maybe I try modify code from link for my own purposes)

Thanks!)

RikKargones | 2021-03-13 22:18