How do you add an item or another submenu to a submenu?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By blurymind
:warning: Old Version Published before Godot 3 was released.

I’ve been trying to figure out the syntax to do that.

Does anyone know how to
Add new items to a submenu?
Add another submenu to a submenu??

How exactly do you operate on submenus?
Get_node? How?

:bust_in_silhouette: Reply From: mollusca

Here’s some sample code that adds a submenu and a sub-submenu to the PopupMenu of a MenuButton:

extends MenuButton

var popup
var submenu = PopupMenu.new()
var subsubmenu = PopupMenu.new()

func _ready():
	popup = get_popup()
	
	subsubmenu.set_name("subsubmenu")
	subsubmenu.add_item("Sub-submenu item a")
	subsubmenu.add_item("Sub-submenu item b")
	
	submenu.set_name("submenu")
	submenu.add_item("Submenu item a")
	submenu.add_item("Submenu item b")
	submenu.add_child(subsubmenu)
	submenu.add_submenu_item("Sub-submenu", "subsubmenu")
	
	popup.add_item("Item a")
	popup.add_item("Item b")
	popup.add_child(submenu)
	popup.add_submenu_item("Submenu", "submenu")

To check which item the user clicks use the item_pressed signal. You’ll probably want to connect each submenu to a separate function.

Hi,
thank you for the answer. In your example, how do you delete all the submenus before creating them?
Basically I need to reset them every time the menu button is clicked - I want to make submenus populate dynamically every time the button is clicked

My ultimate goal is the get a folder structure and turn it into a submenu structure to access directories of subfolders. It’s turning out to be very challenging :o

blurymind | 2017-08-21 11:51