evennia.contrib.tutorial_examples.red_button¶
This is a more advanced example object. It combines functions from script.examples as well as commands.examples to make an interactive button typeclass.
Create this button with
create/drop red_button.RedButton
Note that you must drop the button before you can see its messages!
Technical¶
The button’s functionality is controlled by CmdSets that gets added and removed depending on the ‘state’ the button is in.
Lid-closed state: In this state the button is covered by a glass cover and trying to ‘push’ it will fail. You can ‘nudge’, ‘smash’ or ‘open’ the lid.
Lid-open state: In this state the lid is open but will close again after a certain time. Using ‘push’ now will press the button and trigger the Blind-state.
Blind-state: In this mode you are blinded by a bright flash. This will affect your normal commands like ‘look’ and help until the blindness wears off after a certain time.
Timers are handled by persistent delays on the button. These are examples of evennia.utils.utils.delay calls that wait a certain time before calling a method - such as when closing the lid and un-blinding a character.
Bases:
evennia.commands.command.CommandPush the red button (lid closed)
- Usage:
push button
This is the version of push used when the lid is closed.
An alternative would be to make a ‘push’ command in a default cmdset that is always available on the button and then use if-statements to check if the lid is open or closed.
Bases:
evennia.commands.command.CommandTry to nudge the button’s lid.
- Usage:
nudge lid
This command will have you try to push the lid of the button away.
Nudge the lid. Random chance of success to open it.
Bases:
evennia.commands.command.CommandSmash the protective glass.
- Usage:
smash glass
Try to smash the glass of the button.
The lid won’t open, but there is a small chance of causing the lamp to break.
Bases:
evennia.commands.command.Commandopen lid
- Usage:
open lid
simply call the right function.
Bases:
evennia.commands.cmdset.CmdSetA simple cmdset tied to the redbutton object.
It contains the commands that launches the other command sets, making the red button a self-contained item (i.e. you don’t have to manually add any scripts etc to it when creating it).
Note that this is given with a key_mergetype set. This is set up so that the cmdset with merge with Union merge type except if the other cmdset to merge with is LidOpenCmdSet, in which case it will Replace that. So these two cmdsets will be mutually exclusive.
Populates the cmdset when it is instantiated.
Bases:
evennia.commands.command.CommandPush the red button
- Usage:
push button
This is the actual executing part of the command. It is called directly after self.parse(). See the docstring of this module for which object properties are available (beyond those set in self.parse())
Bases:
evennia.commands.command.CommandClose the lid
- Usage:
close lid
Closes the lid of the red button.
Close the lid
Bases:
evennia.commands.cmdset.CmdSetThis is the opposite of the Closed cmdset.
Note that this is given with a key_mergetype set. This is set up so that the cmdset with merge with Union merge type except if the other cmdset to merge with is LidClosedCmdSet, in which case it will Replace that. So these two cmdsets will be mutually exclusive.
Setup the cmdset
Bases:
evennia.commands.command.CommandLooking around in darkness
- Usage:
look <obj>
… not that there’s much to see in the dark.
This replaces all the senses when blinded.
Bases:
evennia.commands.command.CommandHelp function while in the blinded state
- Usage:
help
Just give a message while blinded. We could have added this to the CmdBlindLook command too if we wanted to keep things more compact.
Bases:
evennia.commands.cmdset.CmdSetThis is the cmdset added to the account when the button is pushed.
Since this has mergetype Replace it will completely remove the commands of all other cmdsets while active. To allow some limited interaction (pose/say) we import those default commands and add them too.
We also disable all exit-commands generated by exits and object-interactions while blinded by setting no_exits and no_objs flags on the cmdset. This is to avoid the player walking off or interfering with other objects while blinded. Account-level commands however (channel messaging etc) will not be affected by the blinding.
Setup the blind cmdset
Bases:
evennia.objects.objects.DefaultObjectThis class describes an evil red button. It will blink invitingly and temporarily blind whomever presses it.
The button can take a few optional attributes controlling how things will be displayed in its various states. This is a useful way to give builders the option to customize a complex object from in-game. Actual return messages to event-actions are (in this example) left with each command, but one could also imagine having those handled via Attributes as well, if one wanted a completely in-game customizable button without needing to tweak command classes.
Attributes: - desc_closed_lid: This is the description to show of the button
when the lid is closed.
desc_open_lid”: Shown when the lid is open
auto_close_msg: Message to show when lid auto-closes
desc_add_lamp_broken: Extra desc-line added after normal desc when lamp is broken.
blink_msg: A list of strings to randomly choose from when the lamp blinks.
Notes: The button starts with lid closed. To set the initial description, you can either set desc after creating it or pass a desc attribute when creating it, such as button = create_object(RedButton, …, attributes=[(‘desc’, ‘my desc’)]).
This function is called (once) when object is created.
Switches the button to having its lid closed.
- Parameters
msg (str, optional) – If given, display a message to the room
lid closes. (when) –
This will first try to get the Attribute (self.db.desc_closed_lid) in case it was set by a builder and if that was None, it will fall back to self.desc_closed_lid, the default description (note that lack of .db).
Switches the button to having its lid open. This also starts a timer that will eventually close it again.
Someone was foolish enough to press the button! Blind them temporarily.
- Parameters
caller (Object) – The one to be blinded.
Breaks the lamp in the button, stopping it from blinking for a while