You are on page 1of 3

Dialogues

Well be developing this game using the standard C++ input and output stream system, and we will
require at least a random number generator and string, vector, and list classes. We can do without
most other things, so the very top of main.cpp will look like

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12

#include
#include
#include
#include
#include
#include
#include

<iostream>
<string>
<vector>
<list>
<utility>
<cstdlib>
<ctime>

int main(void)
{
return 0;
}

Theres not much more that we can do in main.cpp at the moment, not until we create some classes.
In a typical RPG game, the world is portrayed to the player through the use of images and sound,
and the player moves their character (usually in real-time) accordingly. We dont have these luxuries,
and so must find a different way of portraying the world, which we will do through the use of
dialogues. A dialogue is merely a question and a series of choices; we will output some text
describing the situation and asking the player to act, and then we will present the player with a series
of options, one of which they will pick. The world will then change depending on the action chosen,
and the cycle starts again. Using this dialogue system the game code can be split into small units;
perfect for an object-orientated language such as C++. Lets create dialogue.hpp and begin to
create the dialogue class.

dialogue.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

#ifndef DIALOGUE_HPP
#define DIALOGUE_HPP
#include <string>
#include <vector>
#include <iostream>
// Gameplay is expressed using dialogues, which present a piece of
// information and some responses, and the ask the user to pick one.
// If they do not pick a valid one then the dialogue loops until
// they do
class Dialogue
{
private:
public:

16
17
18
19
20
21
22
23

Dialogue()
{
}
};
#endif /* DIALOGUE_HPP */

The dialogue needs to contain a question/statement, and then a series of choices. Since were using
text, strings seem a good fit. These will be private variables, so place them in the private section

dialogue.hpp
1
2
3
4
5
6

// Initial piece of information that the dialogue displays


std::string description;
// A vector of choices that will be outputted. No numbering is
// necessary, the dialogue does that automatically
std::vector<std::string> choices;

I mentioned at the beginning of the tutorial that a passing familiarity with the STL would be quite
useful. Well were barely into the program and an STL container has already cropped up! If youve
never seen code like this before, a vector is a class that behaves much like a resizeable array that
can have items added and removed from it. This means that we dont have to specify its size upon
declaring it; perfect for in a class where we dont know how it will be used in advance. The variable
type in the angle brackets is the type that the vectorstores. The analogous code for a standard
array would be

std::string choices[someNumber];

Now well need a constructor for the Dialogue (keep the empty constructor, this is a new one). Being
a constructor it should be placed inside the public section

dialogue.hpp
1
2
3
4
5

Dialogue(std::string description, std::vector<std::string> choices)


{
this->description = description;
this->choices = choices;
}

In order for the dialogue to be used once it has been created, it must be activated. This allows us to
create dialogues in advance of them being used and allow us to run them multiple times if
necessary. The activation function will output the description and then present the player with the
choices, automatically numbering them for convenience. The player will then enter one of the
numbers and the dialogue will return the number of the option they selected. Whilst that may seem a
little simple to but into its own function, let alone its own class, we also make sure that the number
the user inputted is a valid one. This stops us from repeatedly coding error checking
code. activate() is a public function

dialogue.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

// Run the dialogue


int activate()
{
// Output the information
std::cout << description << std::endl;
// Output and number the choices
for(int i = 0; i < this->choices.size(); ++i)
{
std::cout << i+1 << ": " << this->choices[i] << std::endl;
}
int userInput = -1;
// Repeatedly read input from stdin until a valid option is
// chosen
while(true)
{
std::cin >> userInput;
// 'Valid' means within the range of numbers outputted
if(userInput >= 0 && userInput <= choices.size())
{
return userInput;
}
}
return 0;
}

Thats the dialogue class done! This class will form the basis of almost all game-player interaction.

You might also like