+2 votes

According to the doc

  • Add a custom menu to 'Project > Tools' as name that calls callback on
    an instance of handler with a parameter ud when user activates it.

so I have the following code

tool
extends EditorPlugin

var test_obj = load("res://addons/Add_item_to_tool/tool_test_ui.tscn")
var instanced_ui = null 
func _enter_tree():

    add_tool_menu_item("test_tool_menu",test_obj,"instance_ui")


func _exit_tree():
    remove_tool_menu_item("test_tool_menu")
    instanced_ui.queue_free()

func instance_ui():
    instanced_ui = load("res://addons/Add_item_to_tool/tool_test_ui.tscn").instance()

if I understand it correctly . I need a string name for the menu, and a obj and a fcuntion to call when I click on the menu. But I am getting
editor/editor_node.cpp:2437 - Error calling function from tool menu: 'PackedScene::instance_ui': Method not found.
I am not sure how to use this, and there doesn't seem to have any example on it

asked Jan 14 in Engine by lowpolygon (166 points)

1 Answer

+1 vote
Best answer

So, I have also been looking into how to add items to the Project/Tool menu. So far I have found that:

  1. The Object add_tool_menu_item takes must be loaded from a
    script. In other words, loading a scene (as far as I know) will not
    work.

  2. The object passed to add_tool_menu_item must have the function

Here's an example add_tool_mean_item being used to fire a method and display a file popup:

tool
extends EditorPlugin

var popup
func _enter_tree():
    popup = FileDialog.new()
    add_tool_menu_item("Test", self, "callback")
    # add_import_plugin(import_plugin)

func callback(ud): 
    add_child(popup)
    popup.popup()

func _exit_tree():
    remove_tool_menu_item("Test")
    popup.free()

You add your GUI items to the editor once your callback method has been fired using add_child.

answered Jan 15 by Capital-EX (52 points)
selected Jan 15 by lowpolygon

Thanks, this works. I now just need to implement this into my tool

Also use get_editor_interface().get_base_control().add_child instead of just add_child. You GUI elements will automatically inherit the current editor them get_editor_interface().get_base_control().add_child.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.