Package parpg :: Module dialogueprocessor :: Class DialogueProcessor
[hide private]
[frames] | no frames]

Class DialogueProcessor

source code

object --+
         |
        DialogueProcessor

Primary interface to the dialogue subsystem used to initiate and process a Dialogue with an NPC.

To begin a dialogue with an NPC a DialogueProcessor must first be instantiated with the dialogue data to process and a dictionary of Python objects defining the game state for testing of response conditionals. The initiateDialogue must be called to initialized the DialogueProcessor, and once it is initialized processing of DialogueSections and DialogueResponses can be initiated via the continueDialogue and reply class methods.

The state of dialogue processing is stored via the dialogue_section_stack class attribute, which stores a list of DialogueSections that have been or are currently being processed. Each time reply is called with a DialogueResponse its next_section_id attribute is used to select a new DialogueSection from the dialogue. The selected DialogueSection is then pushed onto the end of the dialogue_section_stack, ready to be processed via continueDialogue. The exception to this rule occurs when reply is called with a DialogueResponse whose next_section_id attribute is "end" or "back". "end" terminates the dialogue as described below, while "back" removes the last DialogueSection on the dialogue_section_stack effectively going back to the previous section of dialogue.

The DialogueProcessor terminates dialogue processing once reply is called with a DialogueResponse whose next_section_id == 'end'. Processing can also be manually terminated by calling the endDialogue class method.


Note: See the dialogue_demo.py script for a complete example of how the DialogueProcessor can be used.

Instance Methods [hide private]
 
__init__(self, dialogue, game_state)
Initialize a new DialogueProcessor instance.
source code
DialogueSection
getDialogueGreeting(self)
Evaluate the RootDialogueSections conditions and return the valid DialogueSection which should be displayed first.
source code
 
initiateDialogue(self)
Prepare the DialogueProcessor to process the Dialogue by pushing the starting DialogueSection onto the dialogue_section_stack.
source code
 
continueDialogue(self)
Process the L{DialogueSection} at the top of the L{dialogue_section_stack}, run any L{DialogueActions<DialogueActions>} it contains and return a list of valid L{DialogueResponses<DialogueResponses> after evaluating any response conditionals.
source code
DialogueSection
getCurrentDialogueSection(self)
Return the DialogueSection at the top of the dialogue_section_stack.
source code
 
runDialogueActions(self, dialogue_node)
Execute all DialogueActions contained by a DialogueSection or DialogueResponse.
source code
list of DialogueResponses
getValidResponses(self, dialogue_section)
Evaluate all DialogueResponse conditions for a DialogueSection and return a list of valid responses.
source code
 
reply(self, dialogue_response)
Reply with a DialogueResponse, execute the DialogueActions it contains and push the next DialogueSection onto the dialogue_section_stack.
source code
 
endDialogue(self)
End the current dialogue and clean up any resources in use by the DialogueProcessor.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Variables [hide private]
  _logger = logging.getLogger('dialogueengine.DialogueProcessor')
Instance Variables [hide private]
Dialogue dialogue
dialogue data currently being processed.
list of DialogueSections dialogue_section_stack
sections of dialogue that have been or are currently being processed.
dict of Python objects game_state
objects defining the game state that should be made available for testing DialogueResponse conditionals.
Bool

Usage: >>> game_state = {'pc': player_character, 'quest': quest_engine} >>> dialogue_processor = DialogueProcessor(dialogue, game_state) >>> dialogue_processor.initiateDialogue() >>> while dialogue_processor.in_dialogue: ... valid_responses = dialogue_processor.continueDialogue() ... response = choose_response(valid_responses) ... dialogue_processor.reply(response)

in_dialogue
whether a dialogue has been initiated.
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, dialogue, game_state)
(Constructor)

source code 

Initialize a new DialogueProcessor instance.

Parameters:
  • dialogue (Dialogue) - dialogue data to process.
  • game_state (dict of objects) - objects defining the game state that should be made available for testing DialogueResponse conditions.
Overrides: object.__init__

getDialogueGreeting(self)

source code 

Evaluate the RootDialogueSections conditions and return the valid DialogueSection which should be displayed first.

Returns: DialogueSection
Valid root dialogue section.

initiateDialogue(self)

source code 

Prepare the DialogueProcessor to process the Dialogue by pushing the starting DialogueSection onto the dialogue_section_stack.

Raises:
  • RuntimeError - Unable to determine the root DialogueSection defined by the Dialogue.

continueDialogue(self)

source code 

Process the L{DialogueSection} at the top of the
L{dialogue_section_stack}, run any L{DialogueActions<DialogueActions>}
it contains and return a list of valid
L{DialogueResponses<DialogueResponses> after evaluating any response
conditionals.

@returns: valid responses.
@rtype: list of L{DialogueResponses<DialogueResponse>}

@raise RuntimeError: Any preconditions are not met.

@precondition: dialogue has been initiated via L{initiateDialogue}.

getCurrentDialogueSection(self)

source code 

Return the DialogueSection at the top of the dialogue_section_stack.

Returns: DialogueSection
section of dialogue currently being processed.
Raises:
  • RuntimeError - Any preconditions are not met.

Precondition: dialogue has been initiated via initiateDialogue and dialogue_section_stack contains at least one DialogueSection.

runDialogueActions(self, dialogue_node)

source code 

Execute all DialogueActions contained by a DialogueSection or DialogueResponse.

Parameters:
  • dialogue_node (DialogueNode) - section of dialogue or response containing the DialogueActions to execute.

getValidResponses(self, dialogue_section)

source code 

Evaluate all DialogueResponse conditions for a DialogueSection and return a list of valid responses.

Parameters:
  • dialogue_section (DialogueSection) - section of dialogue containing the DialogueResponses to process.
Returns: list of DialogueResponses
responses whose conditions were met.

reply(self, dialogue_response)

source code 

Reply with a DialogueResponse, execute the DialogueActions it contains and push the next DialogueSection onto the dialogue_section_stack.

Parameters:
  • dialogue_response (DialogueReponse) - response to reply with.
Raises:
  • RuntimeError - Any precondition is not met.

Precondition: initiateDialogue must be called before this method is used.


Instance Variable Details [hide private]

dialogue

dialogue data currently being processed.
Get Method:
unreachable.fget(self)
Set Method:
unreachable.fset(self, dialogue)

dialogue_section_stack

sections of dialogue that have been or are currently being processed.
Get Method:
unreachable.fget(self)
Set Method:
unreachable.fset(self, new_value)

game_state

objects defining the game state that should be made available for testing DialogueResponse conditionals.
Get Method:
unreachable.fget(self)
Set Method:
unreachable.fset(self, new_value)

in_dialogue

whether a dialogue has been initiated.
Get Method:
unreachable.fget(self)
Set Method:
unreachable.fset(self, value)