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

Author Topic: Event - slow?  (Read 1501 times)

0 Members and 1 Guest are viewing this topic.

antton

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Event - slow?
« on: December 18, 2016, 09:17:53 pm »
Hello!

from the beginning i used only if loops and i encountered some problems. I switch to use Event instead. But i feel like the events have some delay(probably bad coding xD). I need someone who can tell what the problem is and how I can make it better without the delay on events :P. When i click with the event code it is delay or I need to spam click for the action to happen.


if code =

sf::Vector2i mousepos = sf::Mouse::getPosition(*window);
       
       
                if (IsThisStrenght = true && mousepos.x > this->getPosition().x && mousepos.x < this->getPosition().x + this->getGlobalBounds().width && mousepos.y > this->getPosition().y && mousepos.y < this->getPosition().y + this->getGlobalBounds().height)
                {
                        if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
                        {

                                        if (this->score->reps >= 10)
                                        {

                                                this->stats->Strength += 1;
                                                this->score->reps -= 10;

                                        }

                        }
                }
 

event code-

       
sf::Vector2i mousepos = sf::Mouse::getPosition(*window);
       
        sf::Event event;
       
        while (window->pollEvent(event))
        {

                if (IsThisStrenght = true && mousepos.x > this->getPosition().x && mousepos.x < this->getPosition().x + this->getGlobalBounds().width && mousepos.y > this->getPosition().y && mousepos.y < this->getPosition().y + this->getGlobalBounds().height)
                {
                        if (event.type == sf::Event::MouseButtonPressed)
                        {
                                if (event.mouseButton.button == sf::Mouse::Left)
                                {

                                        if (this->score->reps >= 10)
                                        {

                                                this->stats->Strength += 1;
                                                this->score->reps -= 10;

                                        }
                                }
                        }
                }
        }

grateful for answer


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Event - slow?
« Reply #1 on: December 18, 2016, 09:36:35 pm »
Events and real-time inputs do behave differently, however you can more or less simulate the real-time inputs with events. I suggest to read both the event and the input tutorial.

Events are basically triggered when you press or a release a key/mouse button, while with real time inputs, you just check the current state. So if you want "do something as long as this button is pressed" than it will work right away with real time inputs, but for events you need a variable to store the state, i.e. you set it when the pressed event happens and your reset it when the released event happens.

Alternatively you can also check out Thor's action system, but if you're just starting out, this might seem a bit too complex.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

antton

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Event - slow?
« Reply #2 on: December 18, 2016, 10:07:34 pm »
Thanks for the answer. I will read the Thors action system and read more about real time inputs and events. I have also started out, so everything is not easy xD

 

anything