Dialog System¶
The dialog module provides conversation trees, dialog nodes, and session management for NPC interactions.
Dialog Trees¶
Dialog system for conversations and choice trees.
This module provides a flexible dialog system for NPC conversations, branching narratives, and interactive storytelling.
- class barebones_rpg.dialog.dialog.DialogConditions[source]¶
Bases:
objectHelper class with common dialog condition factories.
This provides reusable condition functions for common dialog scenarios, reducing boilerplate in dialog tree creation.
Example
>>> from barebones_rpg.quests.quest import QuestStatus >>> quest_started = DialogConditions.quest_status(quest, QuestStatus.ACTIVE) >>> enemy_dead = DialogConditions.entity_not_in_location(location, "Goblin")
- static quest_status(quest, status)[source]¶
Create a condition that checks if a quest has a specific status.
- Parameters:
quest – Quest object to check
status – QuestStatus to check for
- Return type:
- Returns:
Condition function
- static quest_not_started(quest)[source]¶
Create a condition that checks if a quest hasn’t been started.
- Parameters:
quest – Quest object to check
- Return type:
- Returns:
Condition function
- static quest_active(quest)[source]¶
Create a condition that checks if a quest is active.
- Parameters:
quest – Quest object to check
- Return type:
- Returns:
Condition function
- static quest_completed(quest)[source]¶
Create a condition that checks if a quest is completed.
- Parameters:
quest – Quest object to check
- Return type:
- Returns:
Condition function
- static entity_in_location(location, entity_name)[source]¶
Create a condition that checks if an entity exists in a location.
- static entity_not_in_location(location, entity_name)[source]¶
Create a condition that checks if an entity does NOT exist in a location.
- static all_conditions(*conditions)[source]¶
Create a condition that requires all sub-conditions to be true.
- static any_condition(*conditions)[source]¶
Create a condition that requires any sub-condition to be true.
- class barebones_rpg.dialog.dialog.DialogChoice(**data)[source]¶
Bases:
BaseModelA choice in a dialog tree.
Example
>>> choice = DialogChoice( ... text="Tell me about the quest", ... next_node_id="quest_info" ... )
>>> # With quest integration >>> choice = DialogChoice( ... text="I'll help you!", ... next_node_id="accepted", ... quest_to_start=my_quest ... )
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class barebones_rpg.dialog.dialog.DialogNode(**data)[source]¶
Bases:
BaseModelA node in a dialog tree.
Each node contains text, optional speaker, and choices for the player.
Example
>>> node = DialogNode( ... id="greeting", ... speaker="Village Elder", ... text="Welcome, traveler. How can I help you?", ... choices=[ ... DialogChoice(text="Tell me about the village", next_node_id="village_info"), ... DialogChoice(text="Goodbye", next_node_id=None) ... ] ... )
-
choices:
List[DialogChoice]¶
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- get_available_choices(context)[source]¶
Get all available choices based on current context.
-
choices:
- class barebones_rpg.dialog.dialog.DialogTree(**data)[source]¶
Bases:
BaseModelA complete dialog tree.
Contains all nodes and manages navigation through the dialog.
Example
>>> tree = DialogTree(name="Village Elder Dialog") >>> tree.add_node(DialogNode( ... id="start", ... speaker="Elder", ... text="Hello!", ... choices=[DialogChoice(text="Hi", next_node_id="greeting")] ... )) >>> tree.set_start_node("start")
-
nodes:
Dict[str,DialogNode]¶
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- add_node(node)[source]¶
Add a node to the tree.
- Parameters:
node (
DialogNode) – Node to add- Return type:
- Returns:
True if node was added successfully
- get_node(node_id)[source]¶
Get a node by ID.
- Parameters:
node_id (
str) – ID of node to retrieve- Return type:
- Returns:
Dialog node or None if not found
- get_start_node()[source]¶
Get the starting node.
- Return type:
- Returns:
Start node or None if not set
-
nodes:
- class barebones_rpg.dialog.dialog.DialogSession(dialog_tree, game=None, context=None)[source]¶
Bases:
objectActive dialog session.
Manages the state of an ongoing conversation. The framework automatically populates the context with game systems when a game instance is provided.
Example
>>> tree = DialogTree(name="Test") >>> # ... add nodes ... >>> # Simple - just pass game, framework handles context >>> session = DialogSession(tree, game=game) >>> # Or add custom context >>> session = DialogSession(tree, game=game, context={"npc_mood": "happy"})
- get_available_choices()[source]¶
Get available choices at current node.
- Return type:
- Returns:
List of available choices
Dialog Renderer¶
Dialog UI rendering utilities.
This module provides rendering utilities for dialog trees, including dialog boxes, speaker names, text with word wrapping, and choice buttons.
- class barebones_rpg.dialog.dialog_renderer.DialogRenderer(renderer, screen_width, screen_height)[source]¶
Bases:
objectRenderer for dialog UI.
This class handles rendering of: - Dialog boxes with borders - Speaker names - Dialog text with word wrapping - Choice buttons with hover effects - Click handling for choices
- Parameters:
- is_mouse_over_choice(mouse_pos, choice_index, num_choices)[source]¶
Check if mouse is over a choice button.
- handle_click(event, session)[source]¶
Handle a mouse click on the dialog.
- Parameters:
event (
Event) – Pygame mouse button eventsession (
DialogSession) – The active dialog session
- Return type:
- Returns:
True if a choice was selected
- render_speaker(speaker_name)[source]¶
Render the speaker name.
- Parameters:
speaker_name (
str) – Name of the speaker
- render_text(text)[source]¶
Render the dialog text with word wrapping.
- Parameters:
text (
str) – The text to render
- render_choices(session)[source]¶
Render the dialog choices as buttons.
- Parameters:
session (
DialogSession) – The active dialog session
- render_session(session)[source]¶
Render a complete dialog session.
This is a convenience method that renders all dialog components.
- Parameters:
session (
DialogSession) – The dialog session to render
- render_with_overlay(session, overlay_alpha=180)[source]¶
Render dialog with a semi-transparent overlay over the world.
This creates a darkened background effect when dialog is active.
- Parameters:
session (
DialogSession) – The dialog session to renderoverlay_alpha (
int) – Alpha value for the overlay (0-255)
- configure(dialog_box_height=None, dialog_box_margin=None, choice_height=None, choice_width=None, bg_color=None, speaker_color=None, speaker_font_size=None, text_font_size=None, choice_font_size=None, choice_y_start_offset=None, text_y_offset=None, text_line_spacing=None)[source]¶
Configure dialog renderer appearance.