closing menus

Please post any questions about developing your plugin here. Please use the search function before posting!
User avatar
velocity
Senior Member
Posts: 220
Joined: Sat May 10, 2014 6:17 pm

closing menus

Postby velocity » Mon Feb 27, 2017 6:52 pm

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?
User avatar
Ayuto
Project Leader
Posts: 2193
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: closing menus

Postby Ayuto » Mon Feb 27, 2017 7:41 pm

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.
User avatar
velocity
Senior Member
Posts: 220
Joined: Sat May 10, 2014 6:17 pm

Re: closing menus

Postby velocity » Tue Feb 28, 2017 1:36 pm

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...
User avatar
Ayuto
Project Leader
Posts: 2193
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: closing menus

Postby Ayuto » Tue Feb 28, 2017 7:22 pm

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()

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 25 guests