how does add_tool_menu_item work ?

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

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

:bust_in_silhouette: Reply From: Capital-EX

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.

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

lowpolygon | 2020-01-15 06:44

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.

Capital-EX | 2020-01-15 11:20