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

Author Topic: sf::clock getting reset  (Read 4041 times)

0 Members and 1 Guest are viewing this topic.

GAMINGBACON97

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
sf::clock getting reset
« on: September 07, 2015, 09:49:09 pm »
how else could I do this without having to restart everytime the function is called?, I'm trying to reset the players position after a certain amount of time
sf::Clock Timer;
sf::Time elapsed;
sf::Time elapsed = Timer.getElapsedTime();
void ResetPosition(){
    Timer.restart();
    if (elapsed.asSeconds() >= 1){
        Player.setPosition(750, 200);
    }
 

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: sf::clock getting reset
« Reply #1 on: September 07, 2015, 09:54:46 pm »
Put Timer.restart() inside the if

GAMINGBACON97

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
Re: sf::clock getting reset
« Reply #2 on: September 07, 2015, 10:40:30 pm »
that didn't help

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: sf::clock getting reset
« Reply #3 on: September 07, 2015, 10:50:34 pm »
If you want this to happen every second then you have to reset the timer each second (inside the if). If you only want it to happen once then don't reset it. If you want something other than that then please say. (are you saying that you want the same effect but without resetting the timer? Then store the time last time the player's position was reset and check the elapsed time against it, but do this in the if). Also remember that the timer starts as soon as it is created so if you are creating it globally then it is timing since before main.

GAMINGBACON97

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
Re: sf::clock getting reset
« Reply #4 on: September 07, 2015, 10:54:57 pm »
I'm moving my character, and then I call this function to set him back at his place after a certain amount of time has passed.

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: sf::clock getting reset
« Reply #5 on: September 07, 2015, 10:59:18 pm »
Yes I know, and my previous post says how to do that. Start the clock just before the game loop. Assuming you are calling the ResetPosition function every frame:
Store current elapsed time.
If it is greater than the amount of time between setting off and resetting position
  Reset position
  Reset clock

If you don't want to reset the clock but do want to do this repeatedly
If current elapsed time is more than time between resets + time recorded at last reset
  Reset position
  Store current elapsed time

GAMINGBACON97

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
Re: sf::clock getting reset
« Reply #6 on: September 07, 2015, 11:02:18 pm »
that is really confusing how you said it, could you add a bit of code so I know what you are saying?

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: sf::clock getting reset
« Reply #7 on: September 07, 2015, 11:06:31 pm »
As a quickly whipped up and untested bit of code
sf::Clock timer;
while(window.isOpen(){//Game loop

//Check events

//Move the player

//This is what could be put in the ResetPosition function
  if(timer.getElapsedTime().asSeconds >= 10) {//This will happen every 10 seconds
    Player.setPosition(750,200);
    timer.restart();
  }

//Draw things

}
 

That's the simplest method I can think of.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::clock getting reset
« Reply #8 on: September 07, 2015, 11:06:52 pm »
GAMINGBACON97, please show a bit of initiative and don't just ask for 1:1 copy-paste code. When shadowmouse is responding with extensive posts, you could at least take the time and try to understand what he meant instead of dismissing everything with one-liners...

Same here and here, by the way. Posting big chunks of code and asking people to find the error is not too nice. See also this.
« Last Edit: September 07, 2015, 11:10:26 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

GAMINGBACON97

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
Re: sf::clock getting reset
« Reply #9 on: September 07, 2015, 11:23:07 pm »
Nexus, I'm trying to understand what he is saying, but I understand code better than English my native language, and I'm not trying to be rude, It just sort of happens, I'm very thankful that he is even taking the time to help me, and I don't want copy and paste code, because if I do, then I'm not able to repeat the process later because I don't know what I'm doing, and also plagiarizing other's code




GAMINGBACON97

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
Re: sf::clock getting reset
« Reply #10 on: September 07, 2015, 11:42:33 pm »
I feel really dumb right now, nothing I do works, and I can't think of anything to make it work, you example doesn't work, it gives me a whole list of errors, and when I tried to fix them I got even more errors, I will explain my problem in a lot of depth, I want to move my character(done), and then after 2 seconds of him being in his new location to go back to the original location(750, 200), but the thing is he can be in two possible locations,(750, 200 move 10, 0)or(750, 200 move -10, 0) so basically left or right, what currently happens with my code is he does not ever move, if I take out the reset, and if I put in the reset it just moves wherever without going back to original location

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: sf::clock getting reset
« Reply #11 on: September 08, 2015, 08:34:58 am »
Can you post your whole code and errors.
« Last Edit: September 08, 2015, 08:38:07 am by shadowmouse »