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

Started by
23 comments, last by phil05 20 years, 1 month ago
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..."
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.
Can you write a full example?
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]
Thanks
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?
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;} 

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;} 
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.
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.

This topic is closed to new replies.

Advertisement