EditorInterface in Godot – Complete Guide

Creating extensions and plugins for an editor can provide powerful customization options for developers. When working with the Godot engine, a well-known and loved tool for indie game developers, there exists a class known as EditorInterface. This class serves as a bridge to the heart of Godot’s editor, unlocking a myriad of functionalities for personalization and efficiency enhancements.

By learning about the EditorInterface, you open up a world of possibilities for how you can interact with and manipulate the Godot editor to your liking. This knowledge is not only powerful for creating a personal workflow but also essential for developing custom tools that can be shared with the Godot community. Whether you’re new to Godot or an experienced developer looking to expand your toolkit, understanding EditorInterface is a valuable skill to have in your arsenal.

What Is EditorInterface?

The EditorInterface class is a key component of Godot’s extensible architecture. As a part of the scripting API for the editor, it allows developers to access and modify the look and functionality of Godot’s editor window. It includes methods for handling scenes, rendering previews, editing resources and script files, managing editor settings, and more.

What Can You Do with EditorInterface?

With the EditorInterface class, you can customize and automate tasks within the editor. For example, you can switch the editor into distraction-free mode, save and reload scenes, render mesh previews, or even play your game right from the editor. It’s an essential tool for creating personalized editor workflows or building plugins to extend Godot’s capabilities.

Why Should You Learn About EditorInterface?

Learning about EditorInterface equips you with the ability to tailor the Godot editor to your project’s needs, potentially boosting productivity and making development more enjoyable. Additionally, if you plan to contribute to the wider Godot user base with plugins or want to leverage existing community plugins, knowledge of EditorInterface is invaluable. Let’s embark on this journey to master the manipulation of Godot 4’s editor through the EditorInterface class.

CTA Small Image
FREE COURSES AT ZENVA
LEARN GAME DEVELOPMENT, PYTHON AND MORE
ACCESS FOR FREE
AVAILABLE FOR A LIMITED TIME ONLY

Accessing the Editor Interface and Settings

To begin working with the EditorInterface, you first need to access it through the editor script. Here’s a simple way to do so in Godot:

func _enter_tree():
  var editor = get_editor_interface()

Once you have a reference to the EditorInterface, you can manipulate various parts of the editor. Let’s look at how to manipulate editor settings:

func _enter_tree():
  var editor = get_editor_interface()
  var settings = editor.get_settings()
  settings.set("interface/scene_tabs/restore_scenes_on_load", true)

In this example, we changed a setting to restore scenes when the editor loads. This makes working with multiple scenes more fluid, as your previously opened scenes reappear when you reopen Godot.

Manipulating the Filesystem Dock

The filesystem dock is an essential part of Godot’s editor. You might want to automate its behavior or simply navigate through it via code. Here’s how to get and navigate the filesystem dock:

func _enter_tree():
  var editor = get_editor_interface()
  var file_system_dock = editor.get_file_system_dock()
  file_system_dock.navigate_to_path("res://some_folder") # Change to your path

Now, what if you want to refresh the filesystem dock? This can be important after making changes to the files:

func _enter_tree():
  var editor = get_editor_interface()
  editor.get_file_system_dock().navigate_to_path("res://") # Go to root
  editor.get_file_system_dock().refresh()

Dealing with Scenes

Opening and saving scenes is a frequent task. Using EditorInterface, you can script these operations. The following code demonstrates how to save the current scene and open a new one:

func _enter_tree():
  var editor = get_editor_interface()
  # Saving the current scene
  editor.save_scene()
  # Opening a new scene
  editor.open_scene("res://path_to_scene.tscn")

If you’re making a tool that needs to get the currently active scene, this snippet will be useful:

func _enter_tree():
  var editor = get_editor_interface()
  var current_scene = editor.get_edited_scene_root()
  print(current_scene.get_filename())  # This will print the path to the current scene.

Manipulating the Inspector

The Inspector is another central part of the editor where you modify the properties of your selected objects. Say you want to refresh the Inspector or modify the properties of the currently selected node via script, you can do so like this:

func _enter_tree():
  var editor = get_editor_interface()
  # Refreshing the inspector
  editor.get_inspector().refresh()

  # Modifying properties of the selected node
  var selected_nodes = editor.get_selection().get_selected_nodes()
  if selected_nodes.size() > 0:
    var node = selected_nodes[0]
    node.set("transform/scale", Vector3(2,2,2)) # Scaling it by 2 times.
    editor.get_inspector().refresh()

In this code snippet, we refreshed the Inspector and then scaled the first selected node if there’s any selected in the editor.

Stay tuned for our next installment, where we’ll delve further into the creation of custom docks and tool scripts within Godot’s EditorInterface, further enhancing the power at your fingertips.In Godot, tools and extensions that interact with the editor are paramount for streamlining the development process. As we continue exploring the EditorInterface, we’ll delve into more advanced interactions such as executing commands, working with the script editor, and creating custom docks. Let’s discover the capabilities enabled by the EditorInterface in Godot.

Executing Editor Commands

Godot’s editor comes with numerous commands that we can execute programmatically. For example, to undo or redo an action, you can use the `undo` and `redo` methods.

func _my_undo_function():
  var editor = get_editor_interface()
  editor.undo()

func _my_redo_function():
  var editor = get_editor_interface()
  editor.redo()

These functions might be tied to custom buttons or keyboard shortcuts within a plugin, providing support for undoing and redoing actions that your tool performs.

Interacting with the Script Editor

When developing tools that involve scripting, you may want to automate tasks inside the script editor. Here’s how you can access the current script and insert text at the cursor position:

func _insert_text_into_script():
  var editor = get_editor_interface()
  var script_editor = editor.get_script_editor()
  var current_script = script_editor.get_current_script()

  if current_script:
    script_editor.insert_text_at_cursor("print('Hello, Script Editor!')")

If you find that your script is getting cluttered or you simply want to jump to a specific line, the following function can be quite handy:

func _goto_line_in_current_script(line_number):
  var editor = get_editor_interface()
  var script_editor = editor.get_script_editor()
  script_editor.goto_line(line_number)

Creating Custom Docks

Creating custom docks in Godot can significantly improve the workflow for specific tasks. The code below demonstrates how to create a dock at the bottom panel of the editor:

class MyCustomBottomPanel extends VBoxContainer:
  func _ready():
    add_child(Label.new().set_text("This is a custom bottom panel!"))

func _enter_tree():
  var editor = get_editor_interface()
  editor.add_bottom_panel_item("MyCustomPanel", MyCustomBottomPanel.new(), true)

To remove it when your plugin is disabled, you can use the following line:

editor.remove_bottom_panel_item(MyCustomBottomPanel.new())

You can similarly create docks on the left or right side of the editor window. This can drastically improve the usability of your custom editor tools.

Handling the Output Panel

If your tool needs to communicate with the user, utilizing the Output panel to log messages or display results is a wise choice. This snippet shows how to print a message to the Output panel:

func _print_to_output(message):
  var editor = get_editor_interface()
  editor.log(message)

Using `editor.log()`, anything you pass gets printed to the Output panel, making it a powerful way to debug or notify users of your tool.

These are just a few examples of the powerful functionalities that the EditorInterface unlocks. From managing scenes and the filesystem to creating tailored docks and interacting with the script editor, the EditorInterface allows for the creation of robust and personal development experiences. With these capabilities, Godot’s customizable nature truly shines, empowering developers to craft tools that not only enhance their workflow but can also be contributed back to the community for shared benefits.In the world of Godot development, the ability to automate and enhance the Editor through scripts is essential for efficient game creation. Let’s continue our exploration of the Godot EditorInterface with further code examples and dive deeper into its vast capabilities.

Creating and managing pop-up dialogs is a common need for tool development. You may want your plugin to display information, warnings, or fields for user input. Here’s how to create a simple informational pop-up:

func _show_info_dialog():
  var editor = get_editor_interface()
  editor.show_modal("This is an informational pop-up!")

If you need a more complex dialog with buttons and signals, you can extend ‘AcceptDialog’:

class CustomDialog extends AcceptDialog:
  func _ready():
    add_button("Ok", true)
    rect_min_size = Vector2(300,100)
    popup_centered()
    connect("confirmed", self, "_on_confirmed")

  func _on_confirmed():
    print("Dialog confirmed!")

func _show_custom_dialog():
  var custom_dialog = CustomDialog.new()
  # Assuming this function is called within a control node
  add_child(custom_dialog)
  custom_dialog.show()

For when you want to expose custom properties for the nodes your tool works with, the Export variables come in handy. When a script is attached to a node in Godot, export variables appear automatically in the Inspector. Here’s how you’d define and set such a variable from another script:

export (int) var my_custom_integer

func _on_my_button_pressed():
  var selected_objects = get_editor_interface().get_selection().get_selected_nodes()
  if selected_objects.size() > 0:
    var node = selected_objects[0]
    if node.has_script() and node.script.has_method("set_my_exported_var"):
      node.script.call("set_my_exported_var", 42)

This will call a method on the selected node’s script, potentially changing the exported variable’s value.

For tasks involving node instantiation, EditorInterface also offers ways to do it with an undo support:

func _instantiate_prefab(prefab):
  var instance = prefab.instance()
  get_parent().add_child(instance)
  get_editor_interface().get_undo_redo().add_do_method(instance, "free")

This code snippet creates an instance of a prefab and ensures that if the action is undone, the instantiated node is freed.

Sometimes, you’ll want to make sure the user does not lose any unsaved changes:

func _safe_close_scene():
  var editor = get_editor_interface()
  if editor.get_unsaved_scenes().size() > 0:
    editor.save_all_scenes()
  editor.close_current_scene()

This example first checks for unsaved scenes, saves them, and then closes the current scene.

Lastly, for truly advanced tool building, you might need to apply custom shaders or materials to the editor’s viewport. Here’s the code to apply a custom material to the currently selected MeshInstance:

func _apply_material_to_selected(material):
  var editor = get_editor_interface()
  var selected_nodes = editor.get_selection().get_selected_nodes()
  for node in selected_nodes:
    if node is MeshInstance:
      node.material_override = material

Mastering these techniques allows you to create Editor tools and plugins that can range from simple productivity aids to full-fledged subsystems tailored to your pipeline. As you become more familiar with the EditorInterface, you’ll discover that your ability to mold the Godot Editor to your specific needs is almost limitless. Dive in, experiment, and contribute back to the Godot community with the tools you develop!

Where to Go Next on Your Godot Journey

Your adventures with Godot and its EditorInterface are just beginning, and there’s a whole world of game development waiting for you to explore. At Zenva, we’re here to guide you every step of the way on your journey from beginner to professional game developer.

For those looking to dive deeper into the Godot engine and expand your game development capabilities, our Godot Game Development Mini-Degree is the perfect next step. This comprehensive collection of courses will teach you the ins and outs of creating cross-platform games using the latest version of Godot. You’ll learn through practical projects across various game genres, building a portfolio that showcases your newfound skills. Whether you’re a beginner or looking to enhance your development expertise, this Mini-Degree offers the flexibility to learn at your own pace and the depth to master critical game development concepts.

In addition, for a broad collection of resources that can assist you further, be sure to check out our other Godot courses. These courses cover a wide spectrum of topics, helping you flesh out your knowledge and apply it to creating your dream games. With Zenva, your path to mastering game development is clear, accessible, and filled with possibilities. So why wait? Continue your learning journey with us today, and take your Godot skills to the next level!

Conclusion

Embracing the power of Godot’s EditorInterface is akin to unlocking a secret level in game development – one where your efficiency soars, and your creative visions become reality with less friction. Armed with the knowledge of automating tasks, customizing the editor, and crafting plugins, you are now equipped to shape the Godot Editor to serve your unique workflows. The EditorInterface is more than a tool; it’s a companion that adapts to your journey and grows with your skills.

At Zenva, we’re committed to supporting your growth as a Godot developer, and our Godot Game Development Mini-Degree is the perfect companion to take that next big leap. With a world of learning at your fingertips, the challenge isn’t finding the resources—it’s deciding what amazing project you’ll build next. Continue crafting, learning, and excelling with the Mini-Degree as your guide, and let’s transform the gaming world together, one line of Godot code at a time.

FREE COURSES
Python Blog Image

FINAL DAYS: Unlock coding courses in Unity, Godot, Unreal, Python and more.