A menus package is now available!
Posted: Thu Jul 17, 2014 6:34 pm
We have finally added a menu package! Below you can find some examples:
#1
#2
#3
If you have any suggestions or found a bug, please let us know about it! :)
#1
Syntax: Select all
# =============================================================================
# >> IMPORTS
# =============================================================================
# Source.Python
from menus import SimpleMenu
from menus import Option
from menus import Text
from players.entity import PlayerEntity
from players.helpers import index_from_userid
from messages import SayText2
from events import Event
# =============================================================================
# >> GAME EVENTS
# =============================================================================
@Event
def player_say(event):
'''
Called whenever a player says something.
'''
# Did the player say "apples"?
if event.get_string('text') == 'apples':
# Send him the apple menu
my_menu.send(index_from_userid(event.get_int('userid')))
# =============================================================================
# >> CALLBACKS
# =============================================================================
def my_select_callback(menu, ply_index, option):
'''
Called whenever a selection was made.
'''
# Get a PlayerEntity instance
player = PlayerEntity(ply_index)
# Does the player like apples?
if option.value:
SayText2(message='{0} likes apples!'.format(player.name)).send()
else:
SayText2(
message='Everyone likes apples! Go and select "Yes!".').send(
ply_index)
# Return the menu. It will be instantly displayed again.
return menu
# =============================================================================
# >> GLOBAL VARIABLES
# =============================================================================
# Create a new menu
my_menu = SimpleMenu()
# Add a simple line
my_menu.append(Text('Do you like apples?'))
# Add two answer options. The first argument is the text that is displayed
# in the menu. The second argument is a value that you can access later in
# your select callback
my_menu.append(Option('Yes!', True))
my_menu.append(Option('No!', False))
# Define the select callback
my_menu.select_callback = my_select_callback
#2
Syntax: Select all
# =============================================================================
# >> IMPORTS
# =============================================================================
# Python
import time
# Source.Python
from menus import SimpleMenu
from menus import Option
from menus import Text
from players.entity import PlayerEntity
from players.helpers import index_from_userid
from events import Event
# =============================================================================
# >> GAME EVENTS
# =============================================================================
@Event
def player_say(event):
'''
Called whenever a player says something.
'''
# Did the player say "menu"?
if event.get_string('text') == 'menu':
# Send the menu
my_menu.send(index_from_userid(event.get_int('userid')))
# =============================================================================
# >> CALLBACKS
# =============================================================================
def my_select_callback(menu, ply_index, option):
'''
Called whenever a selection was made.
'''
# Since only one option was added to the menu, we don't need to make any
# if/elif/else conditions. We just return the other menu
return another_menu
def my_build_callback(menu, ply_index):
'''
Called before the menu is send to a player.
'''
# Clear the menu, so we have an empty menu
menu.clear()
# Get a PlayerEntity instance
player = PlayerEntity(ply_index)
# Welcome the player
menu.append('Hello, {0}!'.format(player.name))
# Add a submenu link
menu.append(Option('Click here to get another menu.'))
# Add an empty line
menu.append(' ')
# Add a simple line
menu.append('Current time:')
# Also, tell the current time
menu.append(time.strftime('%H:%M:%S'))
# =============================================================================
# >> GLOBAL VARIABLES
# =============================================================================
# Create a menu with a build callback
my_menu = SimpleMenu(
select_callback=my_select_callback,
build_callback=my_build_callback
)
# Create another menu with a single line
another_menu = SimpleMenu(['This is the other menu.'])
#3
Syntax: Select all
# =============================================================================
# >> IMPORTS
# =============================================================================
# Python
import random
# Source.Python
from menus import PagedMenu
from menus import Option
from events import Event
from players.helpers import index_from_userid
# =============================================================================
# >> CALLBACKS
# =============================================================================
def my_select_callback(menu, ply_index, option):
'''
Called whenever a selection was made.
'''
# Shuffle the menu : D
random.shuffle(menu)
# Make it sticky
return menu
# =============================================================================
# >> GLOBAL VARIABLES
# =============================================================================
my_menu = PagedMenu(
title='Welcome menu',
description='Choose an option:',
select_callback=my_select_callback
)
# Add some options
for x in range(40):
my_menu.append(Option('Test {0}'.format(x), x))
# =============================================================================
# >> GAME EVENTS
# =============================================================================
@Event
def player_say(event):
'''
Called whenever a player says something.
'''
# Did the player say "menu"?
if event.get_string('text') == 'menu':
# Send the menu
my_menu.send(index_from_userid(event.get_int('userid')))
If you have any suggestions or found a bug, please let us know about it! :)