menu build callback being called over and over

Please post any questions about developing your plugin here. Please use the search function before posting!
12jdlovins
Junior Member
Posts: 17
Joined: Mon Apr 16, 2018 5:11 am

menu build callback being called over and over

Postby 12jdlovins » Sat Apr 28, 2018 11:59 pm

I must be missing something or menus package is bugged maybe?

Syntax: Select all

def build_menu(menu, player_index):
menu.append(PagedOption("test"))
print("added to menu")


main_menu = PagedMenu(
build_callback=build_menu
)

@SayCommand("!testmenu")
def _menu_command(command, index, team_only):
main_menu.send(index)


results in this in the console and the menu to keep getting more items added to it for some reason.

added to menu
added to menu
added to menu
added to menu
added to menu
added to menu
added to menu
added to menu

From all the examples it seems like its supposed to be as simple as that, what is going on?
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: menu build callback being called over and over

Postby satoon101 » Sun Apr 29, 2018 12:23 am

The menu is not only sent when you call main_menu.send, but also every second after that. The reason for this is because menus automatically drop from players' screens in some games after 4 seconds. To combat that, we send the menu every second.

During send, we rebuild the menu, which will call your build_callback override if supplied as you have above. Because menus inherit from the 'list' type, when you append, you are appending the same option to the menu over and over.

If your menu should always stay the same, there should be no reason to override the build_callback. Just append all your items after you create the menu. Then it will always send the way it was created. If the menu needs to be dynamic, which is where you would actually want to override build_callback, it is best to first clear the menu in your built_callback override and then append all items that should be shown.

Syntax: Select all

def build_menu(menu, player_index):
menu.clear()
if Player(player_index).name == '12jdlovins':
menu.append(PagedOption("test"))
else:
menu.append(PagedOption("test2"))


main_menu = PagedMenu(
build_callback=build_menu
)

@SayCommand("!testmenu")
def _menu_command(command, index, team_only):
main_menu.send(index)


Or, at the very least, if you don't want to clear, check to see if any items you might need to add/remove are already options in the menu.

Syntax: Select all

def build_menu(menu, player_index):
if Player(player_index).name == '12jdlovins':
if option1 not in menu:
menu.append(option1)
if option2 in menu:
menu.remove(option2)
else:
if option2 not in menu:
menu.append(option2)
if option1 in menu:
menu.remove(option1)


main_menu = PagedMenu(
build_callback=build_menu
)
option1 = PagedOption("test")
option2 = PagedOption("test2")


@SayCommand("!testmenu")
def _menu_command(command, index, team_only):
main_menu.send(index)
Image
12jdlovins
Junior Member
Posts: 17
Joined: Mon Apr 16, 2018 5:11 am

Re: menu build callback being called over and over

Postby 12jdlovins » Sun Apr 29, 2018 12:27 am

Thanks for the quick response!

That would make sense why the menu.clear() is there. It didn't click in my head why that was actually there until you said you have to redraw.

Thanks for the examples and info.

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 17 guests