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

Author Topic: Starting off without a blank prompt  (Read 739 times)

0 Members and 1 Guest are viewing this topic.

Daoxin

  • Newbie
  • *
  • Posts: 11
    • View Profile
Starting off without a blank prompt
« on: July 29, 2022, 04:26:23 am »
So I am making a text adventure game with choices. The issue I have is that  I made my text as an array list that goes on as you click to enter to advance the text, but every time you start the game, there is a blank screen until you click enter, and the prompt from the area starts.

When I manually put a text, I have it appearing at the start when you start the game. I tried placing it in other areas but won't go away when pressing enter. I notice if I have it in another class, it would o to the next class and continue, but it has that same issue where the screen will be blank until you click the enter button to show the next prompt.

How do I fix this issue?

Here is a copy of the code I have on my cpp file:

#include "Gameplay.h"
#include "GameText.h"
#include "ChoiceA.h"


Gameplay::Gameplay(std::shared_ptr<Context> &context) : context(context){}

Gameplay::~Gameplay(){}

void Gameplay::Init()
{
    context->assets->AddFont(MainFont, "Assets/Myuniquefont-Regular.otf");
    text.setFont(context->assets->GetFont(MainFont));
    text.setOrigin(context->window->getSize().x/2.f,
                   context->window->getSize().y/2.f);
    text.setPosition(context->window->getSize().x/2.f,
                      context->window->getSize().y/2.f);
    text.setCharacterSize(65);
}

void Gameplay::ProcessInput()
{
    std::string text_array[] = {"There was once a hafling and a dragon."
                                "\nThey were wandering around in secrecy.",
                                "Traveling together and far",
                                "And third text",
                                "option \"A\" or option \"B\""};

    std::string text_array_two[] = {"We here on option A",
                                    "Maybe here too?",
                                    "unkown places but here we are"};

    std::string text_array_twoSideB[] = {"We here on option B",
                                    "Maybe here too? Or maybe not",
                                    "unkown places but here we are"};

    sf::Event event;
    while (context->window->pollEvent(event))
    {
        text.setString("Please click enter to continue the story.");

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

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

            if (event.key.code == sf::Keyboard::Return)
            {
                text.setString("Please click enter to continue the story.");

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

            else if(text_array->back() && event.key.code == sf::Keyboard::A)
            {
//                context->states->Add(std::make_unique<ChoiceA>(context), true);
//                text_counter = 0;
            }

//            else if(text_array->size() -1 && event.key.code == sf::Keyboard::B)
//            {
//                text.setString(text_array_twoSideB[text_counter_two]);
//                text_counter_two++;
//
//            }
         }
    }

}


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

}

void Gameplay::Draw()
{
    context->window->clear();
    context->window->draw(text);
    context->window->display();
}
 

and here is my header file:

#ifndef SFML_PROJECT_GAMEPLAY_H
#define SFML_PROJECT_GAMEPLAY_H

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

class Gameplay : public Engine::State
{
private:
    std::shared_ptr<Context> context;
    sf::Text text;

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

    unsigned int text_counter = 0;
    unsigned int text_counter_two = 0;

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


#endif //SFML_PROJECT_GAMEPLAY_H

 


As you can see, I tried different ways to clear the blank spot, but I keep running a road block with a manual string that won't clear no matter if I tried to destroy it or clear it off the screen.

kojack

  • Sr. Member
  • ****
  • Posts: 300
  • C++/C# game dev teacher.
    • View Profile
Re: Starting off without a blank prompt
« Reply #1 on: July 29, 2022, 01:50:06 pm »
Try moving this line (the one at the top inside of the while loop):
text.setString("Please click enter to continue the story.");
into the Gameplay::Init function.

Right now it's only setting the prompt when an event happens (resizing a window, pressing a key, moving the mouse, etc), not when the program starts. So you don't see it until you do something that triggers an event.

Then you can also remove the second copy of that line in the return key event code and uncomment the two lines below that show the next message.

(Saying this off the top of my head, I haven't tried building the code)

Daoxin

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Starting off without a blank prompt
« Reply #2 on: July 30, 2022, 02:38:03 am »
Thank you it worked! Thank you!