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

Author Topic: Using Events to check for inputs ?  (Read 2192 times)

0 Members and 1 Guest are viewing this topic.

Flaze07

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
    • Email
Using Events to check for inputs ?
« on: April 06, 2018, 04:03:24 pm »
Should I use Events to check for inputs to i.e move my character or should I use the other one ??

i.e
Code: [Select]
        sf::Event event;
        while (win.pollEvent(event))
        {
            if (event.type == sf::Event::KeyPressed)
            {
                if (event.key.code == sf::Keyboard::Space) chara.jump();
            }
        }

or

Code: [Select]
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) chara.jump();

which one is better?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11000
    • View Profile
    • development blog
    • Email
Re: Using Events to check for inputs ?
« Reply #1 on: April 06, 2018, 04:10:35 pm »
Both work, neither is objective better or worse. Pick the one you're more comfortable with writing code for.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Flaze07

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
    • Email
Re: Using Events to check for inputs ?
« Reply #2 on: April 06, 2018, 04:59:06 pm »
Got it, also is it better to
sf::Event event;
        while (win.pollEvent(event))
        {
            if (event.type == sf::Event::KeyPressed)
            {
                if (event.key.code == sf::Keyboard::Space) chara.jump();
            }
        }
 

or

sf::Event event;
        if (win.pollEvent(event))
        {
            if (event.type == sf::Event::KeyPressed)
            {
                if (event.key.code == sf::Keyboard::Space) chara.jump();
            }
        }
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11000
    • View Profile
    • development blog
    • Email
Re: Using Events to check for inputs ?
« Reply #3 on: April 06, 2018, 05:07:22 pm »
You need to use while for the event loop. Check the official tutorial.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Using Events to check for inputs ?
« Reply #4 on: April 06, 2018, 05:46:31 pm »
Quote
Should I use Events to check for inputs to i.e move my character or should I use the other one ??
They are not equivalent. The KeyPressed event happens once when you press the key, whereas isKeyPressed will return true as long as the key is down, ie continuously for a short while. So in the first case your character will jump once, in the second one it will restart its jump continuously until you release the key. It's easy to workaround (do nothing if a jump is already active), but from a design point of view what you need is an event.
Laurent Gomila - SFML developer

Flaze07

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
    • Email
Re: Using Events to check for inputs ?
« Reply #5 on: April 07, 2018, 05:16:00 am »
Yeah, thanks Laurent.
Also if I used while wouldn't that means that when I have an input the whole other section of the program that does some stuff wouldn't run and my program would be stuck at the input section ?