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

Author Topic: Text advancing or getting to the new line of text  (Read 5412 times)

0 Members and 1 Guest are viewing this topic.

Daoxin

  • Newbie
  • *
  • Posts: 11
    • View Profile
Text advancing or getting to the new line of text
« on: May 14, 2021, 11:33:37 pm »
So I have been working on a text-based adventure game where it is heavy on text and choices you make. The main problem is that the text I try to set up does appear in the game but does not advance after clicking enter and been struggling with it. Should I make a new class for the text that will be read off from or put it all in this current class I have made. So far I can make only one text appear if the player clicks enter and that is good. That is what I want but want to delete the text after they click enter and a new text appears. I tried to do if statements, when they click, enter and works but that is WAY TOO MANY if to continue the story. Wondering if maybe I just need to make a new class that if you click enter, a new text appears or something. I know it has to update or something to advance the text string but unsure what it could be.

Here is the code I have made so far in different classes:

GamePlay::GamePlay(std::shared_ptr<Context> &context) : context(context),
AdvanceButton(false)
{

}

GamePlay::~GamePlay()
{

}

void GamePlay::Init()
{
    //rectangle shape of the chat box
    chatBox.setSize({1500, 320});
    chatBox.setFillColor(sf::Color::Black);
    chatBox.setOutlineColor(sf::Color::White);
    chatBox.setOutlineThickness(5.f);
    chatBox.setOrigin(chatBox.getGlobalBounds().width/1,
                        chatBox.getGlobalBounds().height/1);
    chatBox.setPosition(context->window->getSize().x/1.1f,
                        context->window->getSize().y/1.01f);

    //Titlle screen name of the game code
    context -> assets ->AddFont(MainFont, "Assets/Asdonuts.ttf");
    text.setFont(context->assets->GetFont(MainFont));

    text.setCharacterSize(50);
    text.setFillColor(sf::Color::White);
    text.setPosition(context->window->getSize().x/5.7f,
                     context->window->getSize().y/2);
}

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

        else if(event.type == sf::Event::KeyPressed)
        {

            switch(event.key.code)
            {
                case sf::Keyboard::Enter:
                {
                    text.setString("Hey There");

                    text.setString("Welcome here");//ignore these two as I was just texting this part out that it could work like this

                    text.setString("Wow");//ignore this one too



                    break;
                }
            }
        }
    }
}

void GamePlay::Update(sf::Time deltaTime)
{

}

void GamePlay::Draw()
{
    context->window->clear(sf::Color::Black);
    context->window->draw(chatBox);
    context->window->draw(text);
    context->window->display();
}

#pragma once
#include <iostream>
#include <memory>
#include <SFML/Graphics.hpp>
#include "Game.h"
#include "State.h"
#include <sstream>
#include "ConversationText.h"

class GamePlay : public Engine::State
{
private:
    std::shared_ptr<Context> context;
    sf::Sprite WindowBox;
    sf::RectangleShape chatBox;
    sf::Text text;
    std::ostringstream chat;

    bool isSelected = false;
    bool hasLimit = false;
    bool AdvanceButton;
    int limit;

    void inputLogic(int chaTyped);

public:
    GamePlay(std::shared_ptr<Context> &context);
    ~GamePlay();

    void Init() override;

    void ProcessInput() override;
    void Update(sf::Time deltaTime) override;
    void Draw() override;
};

 
« Last Edit: May 17, 2021, 09:39:42 am by eXpl0it3r »

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Text advancing or getting to the new line of text
« Reply #1 on: May 17, 2021, 01:48:35 pm »
i don't know if I really understand your question... you want to change to a new text everytime the user press Enter? if that's the point, I don't think you need a class for simply holding a text. you could simply have an array of texts. example:

#include <SFML/Graphics.hpp>

int main(){
    sf::RenderWindow window(sf::VideoMode(800, 600), "Text Adventure");

    unsigned int text_counter = 0;
    std::string text_array[] = {"This is the first text",
                                "This is the second one",
                                "And third text",
                                "End"};
    sf::Font font;
    if (!font.loadFromFile("automati.ttf"))
        return 1;
    sf::Text text("", font, 20);

    while (window.isOpen()){
        sf::Event event;
        while (window.pollEvent(event)){
            if (event.type == sf::Event::Closed){
                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++;
                }
            }

        }
        window.clear();
        window.draw(text);
        window.display();
    }
    return 0;
}
 
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Daoxin

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Text advancing or getting to the new line of text
« Reply #2 on: May 17, 2021, 07:29:37 pm »
I see what you mean, I might give that a try then and yes that was my question haha sorry about the confusion. I will give the array a try just a bit worried as it is long with choices and having puzzles involved but will see if that will work then. Thank you! :D

Daoxin

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Text advancing or getting to the new line of text
« Reply #3 on: May 17, 2021, 07:37:25 pm »
i don't know if I really understand your question... you want to change to a new text everytime the user press Enter? if that's the point, I don't think you need a class for simply holding a text. you could simply have an array of texts. example:

#include <SFML/Graphics.hpp>

int main(){
    sf::RenderWindow window(sf::VideoMode(800, 600), "Text Adventure");

    unsigned int text_counter = 0;
    std::string text_array[] = {"This is the first text",
                                "This is the second one",
                                "And third text",
                                "End"};
    sf::Font font;
    if (!font.loadFromFile("automati.ttf"))
        return 1;
    sf::Text text("", font, 20);

    while (window.isOpen()){
        sf::Event event;
        while (window.pollEvent(event)){
            if (event.type == sf::Event::Closed){
                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++;
                }
            }

        }
        window.clear();
        window.draw(text);
        window.display();
    }
    return 0;
}
 

Strangely that did not work for me, It stays in the first line from the array. Thanks though but I will try using the array as you said for a bit.

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Text advancing or getting to the new line of text
« Reply #4 on: May 17, 2021, 07:51:22 pm »
well that's weird. are you sure you copy/pasted everything when testing it?
the code is fine, and I tested it again... see video attached

just a bit worried as it is long with choices and having puzzles involved
two suggestions then:
1) make an array (or even better, a std::vector) for each 'level' of your game
2) for puzzles and choices, you could use an approach like in old book series "Choose Your Own Adventure", in which any choice you make will take you to a different page. but instead of pages, you just load a different element from your array/vector.
« Last Edit: May 17, 2021, 07:55:45 pm by Stauricus »
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Daoxin

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Text advancing or getting to the new line of text
« Reply #5 on: May 17, 2021, 08:56:27 pm »
I copied the

unsigned int text_counter = 0;
    std::string text_array[] = {"This is the first text",
                                "This is the second one",
                                "And third text",
                                "End"};
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++;
            }
        }

Parts into mine and yeah only the first part of the array appears only. I even put the array in the same class function I have for the event to see if that was the issue but was not.

GamePlay::GamePlay(std::shared_ptr<Context> &context) : context(context),
AdvanceButton(false)
{

}

GamePlay::~GamePlay()
{

}

void GamePlay::Init()
{
    //rectangle shape of the chat box
    chatBox.setSize({1500, 320});
    chatBox.setFillColor(sf::Color::Black);
    chatBox.setOutlineColor(sf::Color::White);
    chatBox.setOutlineThickness(5.f);
    chatBox.setOrigin(chatBox.getGlobalBounds().width/1,
                        chatBox.getGlobalBounds().height/1);
    chatBox.setPosition(context->window->getSize().x/1.1f,
                        context->window->getSize().y/1.01f);

    //Titlle screen name of the game code
    context -> assets ->AddFont(MainFont, "Assets/Asdonuts.ttf");
    text.setFont(context->assets->GetFont(MainFont));

    text.setCharacterSize(50);
    text.setFillColor(sf::Color::White);
    text.setPosition(context->window->getSize().x/5.7f,
                     context->window->getSize().y/2);


}

void GamePlay::ProcessInput()
{

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

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

void GamePlay::Update(sf::Time deltaTime)
{
//    text.setString("Hello There");
//
//        text.setString("Hello Again");
}

void GamePlay::Draw()
{
    context->window->clear(sf::Color::Black);
    context->window->draw(chatBox);
    context->window->draw(text);
    context->window->display();
}

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Text advancing or getting to the new line of text
« Reply #6 on: May 18, 2021, 12:38:24 pm »
here it is:

void GamePlay::ProcessInput()
{

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

    sf::Event event;
    while(context->window->pollEvent(event))
//[...]
 

you are recreating the 'text_counter' variable and assigning zero to it everytime the user press Return. so it will be always 0, and since the first item of arrays are 0, you are stuck on the first text.

move this variable to inside your GamePlay class, and assign a value to it in the Init() function:
class GamePlay{
unsigned int text_counter;
//[...]
};


void GamePlay::Init()
{
text_counter = 0;
//[...]
}
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Daoxin

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Text advancing or getting to the new line of text
« Reply #7 on: June 06, 2021, 01:47:05 am »
That helped thank you!

 

anything