godotengine / godotengine/godot-docs

Improved docs for EditorContextMenuPlugin with full working example

Open
#10,999 4 comments 3 reactions 0 assignees View on GitHub
area:class reference enhancement topic:plugin
Dominant language
reStructuredText
Stars
5.7k
Forks
3.8k
Avg merge
1d 20h
Merged PRs (30d)
25

Description

**Your Godot version:**

4.4.1

**Issue description:**

EditorContextMenuPlugin docs were insufficient to make a working plugin

**URL to the documentation page (if already existing):**

https://docs.godotengine.org/en/4.4/classes/class_editorcontextmenuplugin.html#class-editorcontextmenuplugin

I struggled for hours before I finally managed to create a working EditorContextMenuPlugin for the scene tree and file system. I propose to add the following examples to the EditorContextMenuPlugin page:

Note that this example might still not be perfect, so please fix as needed such as the typing which is still not entirely clear to me. Also would be nice with an example that uses an icon and perhaps adds a shortcut as well, which wasn't clear how to achieve.

How do I create or get the SHORTCUT here?

```gdscript
func _init():
add_menu_shortcut(SHORTCUT, handle)

func _popup_menu(paths):
add_context_menu_item_from_shortcut("File Custom options", SHORTCUT, ICON)
```

Working sample code:

`scene_context_menu.gd`

```gdscript
class_name SceneContextMenu
extends EditorContextMenuPlugin

func _init():
print("Initialized custom SceneContextMenu")

func _popup_menu(paths: PackedStringArray) -> void:
add_context_menu_item("Import nodes", on_import_selected)
add_context_menu_item("Export node", on_export_selected)

func on_import_selected(paths: PackedStringArray):
print("import assets for", node)

func on_export_selected(paths: PackedStringArray):
print("export assets for", node)
```

`file_system_context_menu.gd`

```gdscript
class_name FileSystemContextMenu
extends EditorContextMenuPlugin

func _init():
print("FileSystemContextMenu: _init() - Initializing context menu.")

func _popup_menu(paths: PackedStringArray) -> void:
add_context_menu_item("Import assets here", on_import_selected)
add_context_menu_item("Export assets", on_export_selected)
print("FileSystemContextMenu: _popup_menu() - Popup menu extended with import and export assets from filetree.")

func on_import_selected(paths: PackedStringArray):
print("file context item handle:", paths)
if not paths.size() == 0:
print("Can only import to a single location")
return
var path = paths[0]
print("import assets to filetree at", path)

func on_export_selected(paths: PackedStringArray):
print("export filetree assets as paths:", paths)
```

`plugin.gd`

```gdscript
@tool
extends EditorPlugin

var file_context_menu_instance: EditorContextMenuPlugin # Instance for FileSystem context menu
var scene_context_menu_instance: EditorContextMenuPlugin # Instance for SceneTree context menu

func _enter_tree():
print("Plugin Enabled")
const FileSysCtxMenu = preload("res://addons/my-plugin/file_context_menu.gd")
const SceneCtxMenu = preload("res://addons/my-plugin/scene_context_menu.gd")

print("Adding context menu plugins")
scene_context_menu_instance = SceneCtxMenu.new()
file_context_menu_instance = FileSysCtxMenu.new()

# add custom ctx menu extension to scene tree context menu
add_context_menu_plugin(EditorContextMenuPlugin.CONTEXT_SLOT_SCENE_TREE, scene_context_menu_instance)

# add custom ctx menu extension to file system tree context menu
add_context_menu_plugin(EditorContextMenuPlugin.CONTEXT_SLOT_FILESYSTEM, file_context_menu_instance)

func _exit_tree():
print("Plugin Disabled")
# Clean up plugin instances
if is_instance_valid(file_context_menu_instance):
remove_context_menu_plugin(file_context_menu_instance)
if is_instance_valid(scene_context_menu_instance):
remove_context_menu_plugin(scene_context_menu_instance)
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the EditorContextMenuPlugin class documentation page linked in the issue and review the supplied scene-tree, file-system, and plugin examples. Document a complete working example, correct unclear typing or behavior, and explain how to provide an icon and obtain or use a shortcut.

Written by the indexing model from the issue text.

Assessment

Domain
documentation, game-dev
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.