Skip to content

Commit fe9cfe4

Browse files
Godot addon: Fix script editor menu in Godot 4.8 (#376)
* Godot addon: Fix script editor menu in Godot 4.8 Signed-off-by: Prakhar Shukla <prakharshukla@outlook.in> * Refactor code to get the last menu button --------- Signed-off-by: Prakhar Shukla <prakharshukla@outlook.in> Co-authored-by: Nathan Lovato <12694995+NathanLovato@users.noreply.github.com>
1 parent 468ca8d commit fe9cfe4

2 files changed

Lines changed: 32 additions & 26 deletions

File tree

  • addons
    • GDQuest_GDScript_formatter_standalone
    • GDQuest_GDScript_formatter

‎addons/GDQuest_GDScript_formatter/menu.gd‎

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,29 +75,32 @@ func update_menu(show_uninstall: bool) -> void:
7575
## Searches for and returns the last menu node in the script editor top menu bar,
7676
## or null if not found.
7777
func _find_last_menu_button(script_editor: Control) -> MenuButton:
78-
# The first child of the script editor should be a VBoxContainer (main container for the script editor main screen)
79-
# Then the first child of that container should be an HBoxContainer (the menu bar)
80-
# This is based on the current structure of Godot's script editor as of Godot 4.5
81-
# Note: We add multiple checks in there with null returns mainly in case something changes in a future
8278
if script_editor.get_child_count() == 0:
8379
return null
8480

85-
var main_container := script_editor.get_child(0)
86-
if not is_instance_valid(main_container) or not main_container is VBoxContainer:
81+
var version := Engine.get_version_info()
82+
var main_container: Control = null
83+
# In 4.8 onward, the script editor looks like this:
84+
# ScriptEditor -> DocumentEditorContainer -> VBoxContainer -> HBoxContainer (menu bar)
85+
if version.major == 4 and version.minor >= 8:
86+
main_container = script_editor.get_child(0).get_child(0)
87+
else:
88+
main_container = script_editor.get_child(0)
89+
90+
if main_container == null or not main_container is VBoxContainer:
91+
printerr("GDScript formatter: main_container is null or not a VBoxContainer")
8792
return null
8893

89-
if main_container.get_child_count() == 0:
90-
return null
91-
92-
var menu_bar := main_container.get_child(0)
93-
if not is_instance_valid(menu_bar) or not menu_bar is HBoxContainer:
94+
var menu_bar := main_container.get_child(0) as HBoxContainer
95+
if menu_bar == null:
96+
printerr("GDScript formatter: menu_bar not found")
9497
return null
9598

9699
# We reached the menu bar. So now we loop through all the children look for
97100
# the last menu button, which would be the last menu in the menu list.
98101
# Note: Here the goal is to insert the menu after the debug menu, but we
99-
# don't check for the button text because "debug" would only work in English
100-
# this should work in any language.
102+
# don't check for the button text because "debug" would only work in English.
103+
# This should work in any language.
101104
var last_menu_button: MenuButton = null
102105
for child in menu_bar.get_children():
103106
if child is MenuButton:

‎addons/GDQuest_GDScript_formatter_standalone/menu.gd‎

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,29 +61,32 @@ func update_menu() -> void:
6161
## Searches for and returns the last menu node in the script editor top menu bar,
6262
## or null if not found.
6363
func _find_last_menu_button(script_editor: Control) -> MenuButton:
64-
# The first child of the script editor should be a VBoxContainer (main container for the script editor main screen)
65-
# Then the first child of that container should be an HBoxContainer (the menu bar)
66-
# This is based on the current structure of Godot's script editor as of Godot 4.5
67-
# Note: We add multiple checks in there with null returns mainly in case something changes in a future
6864
if script_editor.get_child_count() == 0:
6965
return null
7066

71-
var main_container := script_editor.get_child(0)
72-
if not is_instance_valid(main_container) or not main_container is VBoxContainer:
67+
var version := Engine.get_version_info()
68+
var main_container: Control = null
69+
# In 4.8 onward, the script editor looks like this:
70+
# ScriptEditor -> DocumentEditorContainer -> VBoxContainer -> HBoxContainer (menu bar)
71+
if version.major == 4 and version.minor >= 8:
72+
main_container = script_editor.get_child(0).get_child(0)
73+
else:
74+
main_container = script_editor.get_child(0)
75+
76+
if main_container == null or not main_container is VBoxContainer:
77+
printerr("GDScript formatter: main_container is null or not a VBoxContainer")
7378
return null
7479

75-
if main_container.get_child_count() == 0:
76-
return null
77-
78-
var menu_bar := main_container.get_child(0)
79-
if not is_instance_valid(menu_bar) or not menu_bar is HBoxContainer:
80+
var menu_bar := main_container.get_child(0) as HBoxContainer
81+
if menu_bar == null:
82+
printerr("GDScript formatter: menu_bar not found")
8083
return null
8184

8285
# We reached the menu bar. So now we loop through all the children look for
8386
# the last menu button, which would be the last menu in the menu list.
8487
# Note: Here the goal is to insert the menu after the debug menu, but we
85-
# don't check for the button text because "debug" would only work in English
86-
# this should work in any language.
88+
# don't check for the button text because "debug" would only work in English.
89+
# This should work in any language.
8790
var last_menu_button: MenuButton = null
8891
for child in menu_bar.get_children():
8992
if child is MenuButton:

0 commit comments

Comments
 (0)