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

Author Topic: SFML event not working  (Read 1916 times)

0 Members and 1 Guest are viewing this topic.

Loyette

  • Newbie
  • *
  • Posts: 4
    • View Profile
SFML event not working
« on: April 02, 2017, 04:27:05 pm »
I want to have my program so each event is handled by a method in a separate class. These methods are then called in my main loop. Here's my code. I literally just started SFML so this is copied off the tutorial:

                while (source.window.pollEvent(event))
                {
                        if (event.type == sf::Event::TextEntered)
                                eventHandle.eventKeyPress();

                        // Close requested event = close the window
                        if (event.type == sf::Event::Closed)
                                source.window.close();
                               
                }

I have a variable called eventHandle. Here is the method eventKeyPress:

                        if (source.event.text.unicode < 128){
                        std::cout << "ASCII character typed: " << static_cast<char>(source.event.text.unicode) << std::endl;
                        source.shape.move(sf::Vector2f(50, 0));

        }

As you can see, I've literally put what I want the event to do inside that method.

I did some testing in debug mode and I know why it's not working, but I don't know how to fix it. So in my main() I have defined the variable:

sf::Event event;

I'm using that in my main function, but I'm using a different variable in my 'eventKeyPress'. I tried putting 'sf::Event event;' in the source.h class, but it really didn't like it. The computer only likes it if I have the 'sf::Event event;' inside my int main() function.

How do I fix my problem?
« Last Edit: April 02, 2017, 09:15:34 pm by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
SFML event not working
« Reply #1 on: April 02, 2017, 07:41:05 pm »
You pass the event to the function.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Loyette

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: SFML event not working
« Reply #2 on: April 02, 2017, 07:45:12 pm »
You pass the event to the function.

Please could you give an example? I've tried literally everything, including making all the variables accessible to both classes, but it still doesn't work. It doesn't seem to actually be calling my eventKeyPress function
« Last Edit: April 02, 2017, 08:03:03 pm by Loyette »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: SFML event not working
« Reply #3 on: April 02, 2017, 09:17:32 pm »
// Calling code
eventHandle.eventKeyPress(event);

// Function definition
EventHandler::eventKeyPress(sf::Event event)
{
    // ...
}

The point is to not have one place you "save" the event to, but instead just pass the event to the function you're calling.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything