-
Notifications
You must be signed in to change notification settings - Fork 0
Add interactive dialogue system for NPC tutorials #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
84955b4
113eb2e
c4d40d8
b4ba8e9
bcbe8cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,20 @@ | ||
| # @author Daniel McCoy Stephenson | ||
| class NPC: | ||
| def __init__(self, name: str, backstory: str): | ||
| def __init__(self, name: str, backstory: str, dialogue_options: list = None): | ||
| self.name = name | ||
| self.backstory = backstory | ||
| self.dialogue_options = dialogue_options or [] | ||
|
|
||
| def introduce(self): | ||
| """Returns the NPC's introduction text""" | ||
| return f"{self.name}: {self.backstory}" | ||
|
|
||
| def get_dialogue_options(self): | ||
| """Returns list of available dialogue options""" | ||
| return self.dialogue_options | ||
|
|
||
| def get_dialogue_response(self, option_index: int): | ||
| """Returns the response for a specific dialogue option""" | ||
| if 0 <= option_index < len(self.dialogue_options): | ||
| return self.dialogue_options[option_index].get("response", "") | ||
| return "" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -86,3 +86,52 @@ def showDialogue(self, text): | |
| self.divider() | ||
| input(" [ CONTINUE ]") | ||
| self.currentPrompt.text = "What would you like to do?" | ||
|
|
||
| def showInteractiveDialogue(self, npc): | ||
| """Shows an interactive dialogue menu with the NPC""" | ||
| while True: | ||
| self.lotsOfSpace() | ||
| self.divider() | ||
|
Comment on lines
+90
to
+94
|
||
| print(f" Talking with {npc.name}") | ||
| self.divider() | ||
|
|
||
| # Show dialogue options | ||
| dialogue_options = npc.get_dialogue_options() | ||
| if not dialogue_options: | ||
| # Fallback to simple introduction if no options | ||
| print(npc.introduce()) | ||
| self.divider() | ||
| input(" [ CONTINUE ]") | ||
| self.currentPrompt.text = "What would you like to do?" | ||
| break | ||
|
|
||
| print(" What would you like to ask?\n") | ||
| option_list = [] | ||
| for i, option in enumerate(dialogue_options): | ||
| question = option.get("question", f"Option {i+1}") | ||
| print(f" [{i+1}] {question}") | ||
| option_list.append(str(i+1)) | ||
|
|
||
| print(f" [{len(option_list)+1}] [Back]") | ||
| option_list.append(str(len(option_list)+1)) | ||
|
|
||
| choice = input("\n> ") | ||
|
|
||
| if choice in option_list: | ||
| choice_idx = int(choice) - 1 | ||
|
|
||
| # Check if user chose to go back | ||
| if choice_idx == len(dialogue_options): | ||
| self.currentPrompt.text = "What would you like to do?" | ||
| break | ||
|
|
||
| # Show the response | ||
| response = npc.get_dialogue_response(choice_idx) | ||
| self.lotsOfSpace() | ||
| self.divider() | ||
| print(f" {npc.name}: {response}") | ||
| self.divider() | ||
| input(" [ CONTINUE ]") | ||
| else: | ||
| print(" Invalid choice. Try again!") | ||
| input(" [ CONTINUE ]") | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dialogue_options = dialogue_options or []treats an explicitly provided empty list the same asNone, replacing it with a new list instance. If callers pass an empty list intending to populate it later (or rely on object identity), the NPC won’t see those updates. Prefer an explicitNonecheck (and optionally copy the list) so[]remains a valid provided value.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in commit b4ba8e9. Changed to explicit
Nonecheck to preserve empty list identity. Added test to verify behavior.