gpt4 book ai didi

c++ - 创建对话树

转载 作者:行者123 更新时间:2023-11-30 03:47:32 24 4
gpt4 key购买 nike

所以,我目前正在使用 SDL2 进行一个简单的项目,我很难理解对话树的代码。除了这个,我几乎已经完成了所有事情。那么让我们从什么是对话树开始: An example dialogue tree I got off google

因此,对话树基本上是一个分支故事,用户能够响应并影响所问的问题以获得不同的结果。这在游戏中变得越来越流行。

接下来我将解释我所做的事情。我将通过向您展示我的代码摘录来做到这一点:

switch(faze){
case 1:
if(button1Press){
button1Press = false;
textToAdd += "That's nice, I'm glad to hear it.\n";
answer1TextToAdd = "As am I";
answer2TextToAdd = "How are you holding up?";
answer3TextToAdd = "Wassup with you?";
}else if(button2Press){
button2Press = false;
textToAdd += "What do you mean Meh?\n";
answer1TextToAdd = "Meh";
answer2TextToAdd = "Just; Meh...";
answer3TextToAdd = "I've been up and down recently.";
}else if(button3Press){
button3Press = false;
textToAdd += "I'm sorry, you wanna talk about it?.\n";
answer1TextToAdd = "No, I'm fine.";
answer2TextToAdd = "I'll live";
answer3TextToAdd = "Yeah...";
}
break;
case 2:
if(button1Press){
button1Press = false;
textToAdd += "Cool.\n";
answer1TextToAdd = "I am.";
answer2TextToAdd = "Totally.";
answer3TextToAdd = "Yup.";
}else if(button2Press){
button2Press = false;
textToAdd += "Wow.\n";
answer1TextToAdd = ":S";
answer2TextToAdd = "Yup.";
answer3TextToAdd = "I know.";
}else if(button3Press){
button3Press = false;
textToAdd += "Go on...\n";
answer1TextToAdd = "It doesn't matter.";
answer2TextToAdd = "I'm fine";
answer3TextToAdd = "Yeah...";
}
break;

所以,让我解释一下; faze 是定义我们在游戏中的哪个点的变量,当按下其中一个按钮时它会增加,“TextToAdd”不重要,但答案 1、答案 2 和答案 3 是用户的 3 个可能答案可以给。

如你所见,它只响应按钮按下,用户说什么并不重要,响应纯粹基于哪个按钮。这导致对话树看起来更像这样: My diagram

如您所见,它忽略了提出的问题,甚至忽略了答案。我的引擎唯一需要继续的就是按下哪个按钮。

最佳答案

我实际上会组织得更“复杂”。

对话框中的每一步都应该有一个指向下一步的指针,这样你就可以选择,接下来是对话框的一部分。这让一切变得更简单,您甚至可以回到您的树中。

所以Dialogpart类持有:
*指向下一部分的指针
*可行的选择
*选择功能
*绘制选择按钮的功能

像您这样的实现对于小型应用程序来说还可以,但对于更大的游戏或更长的对话。

关于c++ - 创建对话树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33654920/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com