Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Text Base Adventure Game using SFML  (Read 3121 times)

0 Members and 1 Guest are viewing this topic.

Daoxin

  • Newbie
  • *
  • Posts: 11
    • View Profile
Text Base Adventure Game using SFML
« on: December 27, 2021, 05:49:06 am »
So been trying to make a choose your adventure game with choices you make that affect the story. Seems easier when doing it on the IDE but having trouble making it with SFML. I have a menu screen working and getting a new screen with user keypress to advance some text but thinking this might not be an efficient way of what I want to do. This is what I have so far as a snippet:

void Gameplay::ProcessInput()
{
    int choiceCounter = 0;
    int choiceCounterTwo = 0;

    std::string text_array[] = {"This is the first text",
                                "This is the second one",
                                "And third text",
                                "End"};

    std::string text_array_two[] = {"We here",
                                    "Maybe here too?"};

    sf::Event event;
    while (context->window->pollEvent(event))
    {
        if (event.type == sf::Event::Closed)
        {
            context->window->close();
        }

        if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Return)
        {
            if (text_counter < (sizeof(text_array) / sizeof(text_array[0])))
            {
                text.setString(text_array[text_counter]);
                text_counter++;
            }
            else
            {
                text_counter = 0;

                text.setString(text_array[text_counter]);
                text_counter++;
            }

            if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Num1)
            {
                choiceCounter++;
                std::string choiceRes = std::to_string(choiceCounter);
                text.setString("Here is the choice point: " + choiceRes);
            }
            else
            {
                choiceCounterTwo++;
                std::string choiceRes = std::to_string(choiceCounterTwo);
                text.setString("Here is the other choice point: " + choiceRes);
            }
           
        }
    }

}
 

I get why it ignores the text array and goes straight to results as when I take out the keypress with the number 1, it works fine and advances to the next text. Thinking about it. I guess text array isn't a good way to go. So I've been thinking of either making its own class for the actual text story.

So my question is, how do I advance the text based on the user input in another way?

kojack

  • Sr. Member
  • ****
  • Posts: 314
  • C++/C# game dev teacher.
    • View Profile
Re: Text Base Adventure Game using SFML
« Reply #1 on: December 27, 2021, 08:53:35 am »
If you look at the structure of the key checks, you have this:
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Return)
{
        // Return pressed

        if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Num1)
        {
                // Option 1
        }
        else
        {
                // Option 2
        }
}

The second if is inside of the first if. When the first if is true, the key must be Return. Therefore the second if can never be true, the key can't be both Return and Num1 at the same time. Each pass of the event while loop only gets a single key event at a time.
The else is then the chosen path for any situation that isn't pressing Num1, such as no key pressed (but there was one to reach this far) or any key besides Num1. So the else is run because Return isn't Num1.

The simplest change to not immediately run the else code would be:
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Return)
{
}
else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Num1)
{
        // Option 1
}
else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Num2)
{
        // Option 2
}

Or you could group the common part (event was a key press) better into:
if (event.type == sf::Event::KeyPressed)
{
        if(event.key.code == sf::Keyboard::Return)
        {
        }
        else if(event.key.code == sf::Keyboard::Num1)
        {
        }
        else if(event.key.code == sf::Keyboard::Num2)
        {
        }
}
 

For the actual text adventure part, there's a few ways to go. Off the top of my head, for each node in the story you'd have the following data:
-collection of text lines to describe the story node
-list of choices, each of which takes you to another story node.
I'd probably use something like:
class Choice
{
        public:
        std::string choiceText;
        int destinationNode;
};
class Node
{
        public:
        std::vector<std::string> description;
        std::vector<Choice> choices;
};

std::vector<Node> story;

So a choice has destinationNode, which is the index number of the next node (in the story vector) to jump to. Each Node has a vector of strings (what your text_array was doing) and a vector of choices.
Filling that data could be done in code, but having a loading system and maybe a JSON file to store it all would make it easier (well, once you understand JSON loading, it's VERY useful).

Maybe instead of index numbers for nodes, you could store them in an std::map and give each node a text name like "CastleEntrance".

Anyway, those are my half awake ramblings. :)

Daoxin

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Text Base Adventure Game using SFML
« Reply #2 on: January 21, 2022, 05:38:18 am »
Sorry for the late reply and I see, I will give this a try thank you!