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

Author Topic: Permanently changing a variable with sf::Mouse click  (Read 1224 times)

0 Members and 1 Guest are viewing this topic.

Stuff

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Permanently changing a variable with sf::Mouse click
« on: April 13, 2020, 09:31:45 pm »
I've been trying to figure out how to solve this problem for a while and I still can't figure out how to fix it.

I've been looking for a way to permanently change the variable using the sf::Mouse click function:
//example

int main()
{
      int I = 0; // variable I'm going to change
     
      //window, and the game loop here

      if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
        {
            i += 1; // how i'm going to change the variable
        }

      //I is printed to the console to check the value
}
 

so the problem here is when I click my mouse, i is added by one and now is equal to one, here's the thing. Once I stop clicking i turns right back into 0. Is there something i missed over that could have been useful to fix this problem? Anything could help.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Permanently changing a variable with sf::Mouse click
« Reply #1 on: April 13, 2020, 10:01:10 pm »
Your code doesn't show the mistake, please provide a complete and minimal code that reproduces the problem.
Laurent Gomila - SFML developer

Stuff

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Permanently changing a variable with sf::Mouse click
« Reply #2 on: April 14, 2020, 12:22:02 am »
Oh sorry, I put the i variable before the game loop:

//edited example

int main()
{
      //window, and the game loop here

      int I = 0; // variable I'm going to change

        if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
        {
            i += 1; // how i'm going to change the variable
        }

      //I is printed to the console to check the value
}
 
this is what I meant to show

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Permanently changing a variable with sf::Mouse click
« Reply #3 on: April 14, 2020, 08:34:58 am »
That's not exactly what we expect when we ask for a complete and minimal code ;)

But ok... If your variable is local to the loop then it will be initialized again at every new iteration. If you want to make it persistent then declare it above the game loop.
Laurent Gomila - SFML developer