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

Author Topic: Dialogue-Box | Game-Developement with SFML  (Read 3489 times)

0 Members and 1 Guest are viewing this topic.

itsZ3r0x

  • Newbie
  • *
  • Posts: 5
    • View Profile
Dialogue-Box | Game-Developement with SFML
« on: February 23, 2017, 04:29:59 pm »
Hello,

I have a problem. I am trying to write a function that creates a Dialogue Box for my Game. When the time is bigger than 4s, the player should be able to press the Spacebar and a new Text appears. But when I press the spacebar, the second updateText appears. What I´m doing wrong? I call the function in the main Game Loop.
I´m a beginner in creating Games with SFML - so don´t be to strict with me  ;).

Here is my code:




void updateText(String UpdateText, int x, int y, int size, String UpdateText2, int x2, int y2, int size2)
        {
                Time elapsed1 = clock1.restart();
                tempClock1 += elapsed1;
               
                if (tempClock1.asSeconds() > 4)
                {
                        if (Keyboard::isKeyPressed(Keyboard::Space))
                        {
                                MessageBox_text.setString(UpdateText);
                                MessageBox_text.setPosition(x, y);
                                MessageBox_text.setCharacterSize(size);

                                Time elapsed2 = clock2.restart();
                                tempClock2 += elapsed2;

                                if (tempClock2.asSeconds() > 4)
                                {
                                        if (Keyboard::isKeyPressed(Keyboard::Space))
                                        {
                                                MessageBox_text.setString(UpdateText2);
                                                MessageBox_text.setPosition(x2, y2);
                                                MessageBox_text.setCharacterSize(size2);
                                        }
                                }
                        }
                }
        }

 

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Dialogue-Box | Game-Developement with SFML
« Reply #1 on: February 23, 2017, 05:03:50 pm »
If you are seeing your second updateText then that means tempClock2 must be greater than 4 seconds right? Based on the code you showed us, it looks like it will always be at least 4 seconds because you presumably construct the clock2 somewhere outside of this function along with clock1. If 4 seconds have elapsed on clock1, then won't 4 seconds have elapsed on clock2 too?

itsZ3r0x

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Dialogue-Box | Game-Developement with SFML
« Reply #2 on: February 23, 2017, 05:16:15 pm »
But I start clock 2 if clock1 is greater than 4s. So that means: When clock1 is not greater than 4 seconds, clock 2 is at 0 second. Or have I got that wrong?

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Dialogue-Box | Game-Developement with SFML
« Reply #3 on: February 23, 2017, 05:28:01 pm »
You aren't starting clock2 when clock1 is greater than 4s. You are restarting it. It initially starts when you first construct it. When you restart it, it will tell you how much time has passed since it was started or last restarted.

itsZ3r0x

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Dialogue-Box | Game-Developement with SFML
« Reply #4 on: February 23, 2017, 06:07:20 pm »
So I have to create the clock inside the updateText function?

itsZ3r0x

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Dialogue-Box | Game-Developement with SFML
« Reply #5 on: February 23, 2017, 06:14:11 pm »
Here is my edited code:

void updateText(String UpdateText, int x, int y, int size, String UpdateText2, int x2, int y2, int size2)
        {
                Clock clock1;
                Time tempClock1;
                Time elapsed1 = clock1.restart();
                tempClock1 += elapsed1;

                if (tempClock1.asSeconds() > 4)
                {
                        if (Keyboard::isKeyPressed(Keyboard::Space))
                        {
                                MessageBox_text.setString(UpdateText);
                                MessageBox_text.setPosition(x, y);
                                MessageBox_text.setCharacterSize(size);

                                Clock clock2;
                                Time tempClock2;
                                Time elapsed2 = clock2.restart();
                                tempClock2 += elapsed2;

                                if (tempClock2.asSeconds() > 4)
                                {
                                        if (Keyboard::isKeyPressed(Keyboard::Space))
                                        {
                                                MessageBox_text.setString(UpdateText2);
                                                MessageBox_text.setPosition(x2, y2);
                                                MessageBox_text.setCharacterSize(size2);
                                        }
                                }
                        }
                }
        }
 

But when I press spacebar, nothing happens  :-\.

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Dialogue-Box | Game-Developement with SFML
« Reply #6 on: February 23, 2017, 08:20:08 pm »
No, you will need to do larger refactoring to make this work.
Clock clock1;
Time tempClock1;
Time elapsed1 = clock1.restart();
 
You are now recreating your clock every loop and then immediately checking how much time has passed by calling restart. Almost no time will have passed between constructing the clock and checking it. You are also no longer accumulating time, but just getting a single time value and then starting over next loop. This will never equal 4 seconds.

Instead of trying to use 2 clocks in this manner, why not just use one clock and another variable to keep track of what "state" you are in. Use that state variable to determine what you should be drawing. My psuedo code below is just to demonstrate what I'm talking about. In reality you probably want to use something better than just a number to represent state.

if state == 0 then draw nothing
if state == 1 then draw first text
if state == 2 then draw second text

and then elsewhere you would have

if 4 seconds have elapsed and in state 0
   Change to state 1. Reset your time accumulator variable to 0.
if 4 seconds have elapsed and in state 1
   change to state 2

itsZ3r0x

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Dialogue-Box | Game-Developement with SFML
« Reply #7 on: February 23, 2017, 10:35:05 pm »
Thanks for your help, Arcade!  ;).

Now I got another way for the updateText function (your answer brought me to this idea).

Here is the full textbox-class for all who are interested in:


#pragma once
#include <SFML\Graphics.hpp>
#include <iostream>

using namespace sf;

class MessageBox {

public:

        MessageBox()
        {
                t_messageBox.loadFromFile("assets/message_boxes/message_box.png");
                s_messageBox.setTexture(t_messageBox);
                Pixel.loadFromFile("assets/message_boxes/Pixel.ttf");
                MessageBox_text.setFont(Pixel);
        }

        void setText(String text, int x, int y, int size)
        {
                MessageBox_text.setFillColor(Color::White);
                MessageBox_text.setPosition(x, y);
                MessageBox_text.setString(text);
                MessageBox_text.setCharacterSize(size);
        }
       
        void drawMessageBox(RenderWindow &window)
        {
                if (visible == true)
                {
                        window.draw(s_messageBox);
                        window.draw(MessageBox_text);
                }
        }


        void updateText(String UpdateText, String UpdateText2, String UpdateText3, Event &event)
        {
       
                if (event.type == event.KeyReleased)
                {
                        if (event.key.code == Keyboard::Space)
                        {
                                if(currentText <= 2) currentText++;
                                else {
                                        visible = false; //hides the textbox
                                }
                        }
                }

                switch (currentText)
                {
                case 0: MessageBox_text.setString(UpdateText); break;
                case 1: MessageBox_text.setString(UpdateText2); break;
                case 2: MessageBox_text.setString(UpdateText3); break;
                }

        }


private:
        Texture t_messageBox;
        Sprite  s_messageBox;
        Font Pixel;
        Text MessageBox_text;

        bool visible = true;

        int currentText = 0;
};

 

Arcade, thanks for your help! ;D
« Last Edit: February 23, 2017, 10:40:12 pm by itsZ3r0x »