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

Author Topic: Problem using delta time  (Read 2880 times)

0 Members and 1 Guest are viewing this topic.

AdrianHEY

  • Newbie
  • *
  • Posts: 14
    • View Profile
Problem using delta time
« on: April 15, 2021, 12:13:09 pm »
I am trying to make an int function that returns delta time and it doesn't work. Any idea what can I do?
float deltaTime() {
    sf::Clock deltaClock;
        sf::Time dt = deltaClock.restart();
        std::cout << dt.asSeconds() << std::endl;
        return dt.asSeconds();
}
« Last Edit: April 15, 2021, 12:45:53 pm by AdrianHEY »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Problem using delta time
« Reply #1 on: April 15, 2021, 12:30:27 pm »
Quote
I am trying to make an int function withc return delta tiomme and It doesent work. Any ideawhat can I do?
Please read this thread first.
This concerns a lot of your posts here.

And please spend at least 10 seconds formulating a clear question without typos, if you expect people to spend several minutes answering.
« Last Edit: April 15, 2021, 12:38:38 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

kojack

  • Sr. Member
  • ****
  • Posts: 313
  • C++/C# game dev teacher.
    • View Profile
Re: Problem using delta time
« Reply #2 on: April 17, 2021, 04:06:37 am »
Your deltaTime() function is making a new clock every time you call it. The clock starts at zero.
You need to make one clock and keep using it. Either move the clock outside of the function or place the "static" keyword before the clock in the function. (Static on a variable in a function makes it continue to live after the function ends and reuses it next time without recreating it).