Page 1 of 1

closing menus

Posted: Mon Feb 27, 2017 6:52 pm
by velocity
in package menus

How do I close the active menu of the user, so I make sure that only 1 menu is open at the time, without saving the class object of every menu for each player?

Re: closing menus

Posted: Mon Feb 27, 2017 7:41 pm
by Ayuto
This closes the active menu (if there is one) of a specific player.

Syntax: Select all

user_queue = PagedMenu.get_user_queue(<index>)
if user_queue.active_menu is not None:
user_queue.active_menu.close(<index>)
Though, closing the active menu doesn't ensure that only one menu is opened/queued. There could be multiple menus in the queue.

Also, note that the same menu instance can't be sent twice. The menus logic will take care of that and skips menus that are sent a second time.

Re: closing menus

Posted: Tue Feb 28, 2017 1:36 pm
by velocity
But it's never the same menu instance anyway since I'm calling the menu on a function, its a new class object every time right? I understood from EventScripts you give an id aka name for the popup, but apparently not here. So in order for this to work properly I have to save the menu object for every player, seems like a lot work?

Syntax: Select all

def zones_modify_coordinates(self, index):
player = Player(index)
steamid = player.steamid
self.menu = PagedMenu(
title='Title',
top_separator='',
bottom_separator='',
select_callback=self.zones_modify_coordinates_callback
)
self.menu.append(PagedOption('Create Start Zone', 'create_start_zone))
self.menu.send(player.index)



If I call this menu twice on @SayCommand there will be two menus in the queue hm...

Re: closing menus

Posted: Tue Feb 28, 2017 7:22 pm
by Ayuto
We don't use names for our menus, because that was quite annoying in ES and is actually redundant and not OOP. There are multiple ways to solve your issue:
  1. Use a build callback instead of creating a new menu everytime in your function.
  2. Use the caching you mentioned. That shouldn't be that much work like you think.
  3. Create named menus using this code :D

    Syntax: Select all

    from menus import PagedMenu, PagedOption

    class NamedPagedMenu(PagedMenu):
    def __init__(self, name, *args, **kwargs):
    self.name = name
    super().__init__(*args, **kwargs)

    def __eq__(self, other):
    if isinstance(other, NamedPagedMenu):
    return self.name == other.name

    return super().__eq__(other)


    # Test code
    from events import Event

    @Event('player_say')
    def player_say(event):
    text = event['text']

    menu = NamedPagedMenu('my_menu')
    menu.append(PagedOption(text))

    menu.send()