Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

phil05

Expert C++ question in writing keyword questions for NPC's

This topic is 5261 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m looking for a programmer to make me a script for my game. Like in Everquest, the player can type his own questions, and the NPC would respond to certain keywords in a database. I''m interested in the same thing for my C++ text-based game. Can you share how that might be done? Example: Player: "Do you know where I can buy a sword?" NPC: (sees the word ''sword'') "Yes, in fact, I have one right here..."

Share this post


Link to post
Share on other sites
Advertisement
Searching for individual words in strings is easy in C++. Just dump the text in a std::string object and use std::string::find(). You can get fancier by storing the strings and reply text in an a separate file, or running a full blown parser. But if you want to just trigger on inidividual keywords, std::string::find() should be a good place to start.

Share this post


Link to post
Share on other sites

void bobs_dialog(std::string player_text) {
if (player_text.find("sword") != std::string::npos) {
std::cout << "Yes, in fact, I have one right here..." << std::endl;
} else {
std::cout << "*sigh* Times are tough." << std::endl;
}
}

int main(int, char **) {
bobs_dialog("Do you know where I can buy a sword?");
bobs_dialog("I like cows.");
return 0;
}


[edited by - SiCrane on March 19, 2004 10:00:01 PM]

Share this post


Link to post
Share on other sites
I tried out the code just now. Sorry for not being specific, but the player needs to ask the questions aka typing them in, instead of static questions. How can that be redone on the code above?

Share this post


Link to post
Share on other sites
One method would be to replace main() with:

int main(int, char **) {
char buffer[512];
std::cin.getline(buffer, sizeof(buffer));
bobs_dialog(buffer);
return 0;
}

Share this post


Link to post
Share on other sites
I tried this. It worked but it didn''t allow me to continue a question like typing "Do you have a sword?"


#include <iostream>

void bobs_dialog(std::string player_text) {
if (player_text.find("sword") != std::string::npos) {
std::cout << "Yes, in fact, I have offe right here..." << std::endl;
} else {
std::cout << "*sigh* Times are tough." << std::endl;
}
}

int main(int, char **) {
char player_text[200];
std::cin >> player_text;
bobs_dialog(player_text);
//bobs_dialog("I like cows.");
return 0;
}

Share this post


Link to post
Share on other sites
yay it works great. Thanks For a database, would this be set for each NPC? Or one big database? It seems like this could work for each NPC. I''m new to the database fun stuff.

Share this post


Link to post
Share on other sites
That would depend on how you structure the rest of your program. Depending on how things work you migh include all the dialog information as part of a map or room file. Or all the dialog information in a big file. Or each NPC in a different file. I don''t know how your program works so I wouldn''t be the best one to say how you should organize your data files.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!