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

Author Topic: Time and clock  (Read 8450 times)

0 Members and 1 Guest are viewing this topic.

Aklus

  • Newbie
  • *
  • Posts: 3
    • View Profile
Time and clock
« on: January 16, 2015, 10:47:47 pm »
Hi, i am trying to create my first game in SFML but im having problems with Time and clocks.
Main function looks like this:
logic scope;
while(window.isOpen())
{
scope.fire();
}
 

"fire" looks like this:

void logic::fire()
{
    sf::Clock clock;
    sf::Time shot;
    shot = clock.getElapsedTime();
    sf::Time reload = sf::milliseconds(1500);
    std::cout << shot.asMilliseconds();
    if(sf::Mouse::isButtonPressed(sf::Mouse::Left) and shot >= reload )
    {
        std::cout << "Sup m8" <<std::endl;
        clock.restart();
    }
}
 

As you can see i want to allow my player to shoot but only after 1.5 seconds from last shot. The problem is that "shot.asMilliseconds()" is always showing me that his time is 0. Anyone have any idea what am I doing wrong?

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Time and clock
« Reply #1 on: January 16, 2015, 10:51:43 pm »
You're creating the sf::Clock object inside fire(), so it gets destroyed when fire() exits.  Hence you have a brand new clock every single time you call fire().

This is how all objects work in C++, so it's perfectly normal and expected behavior.  If you wanted the clock to "survive" longer than one iteration of the while loop, then create it outside the loop.

Aklus

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Time and clock
« Reply #2 on: January 16, 2015, 11:03:28 pm »
I tried it before, but it didn't work properly for me as well.

logic scope;
sf::Clock clock;
while(window.isOpen())
{
scope.fire(&clock);
}
 

void logic::fire(sf::Clock *clock)
{
    sf::Time shot;
    shot -> clock.getElapsedTime();                   //Here is an error "base operand -> has non-pointer type sf::Time"
    sf::Time reload = sf::milliseconds(1500);
    std::cout << shot.asMilliseconds();
    if(sf::Mouse::isButtonPressed(sf::Mouse::Left) and shot >= reload )
    {
        std::cout << "Sup m8" <<std::endl;
        clock -> restart();
    }
}
 

What am i doing wrong now?

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Time and clock
« Reply #3 on: January 16, 2015, 11:08:42 pm »
The error tells you exactly what's wrong: You used the -> operator on "shot", which is not a pointer.  Also, in that code "clock" is a pointer so you should be using -> on that, but you aren't.

This is considered very basic C++, and is not unique to SFML in any way, so you may want to spend more time learning the C++ language before trying to use a library written in that language.  I'd suggest reading a good C++ book.

Aklus

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Time and clock
« Reply #4 on: January 16, 2015, 11:46:44 pm »
I'm actually reading a c++ book, I just want to remember everything by programming very simple game. :)

I just forgot about changing "." to "->" in "clock.getElapsedTime". That was a thing which was I doing wrong before.

 Thanks for the reply.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Time and clock
« Reply #5 on: January 18, 2015, 12:07:26 am »
You can pass by reference instead of by pointer, which is generally prefered, if possible. This also has the side-effect of allowing you to use the original code instead of specific pointer syntax.

i.e.
void logic::fire(sf::Clock& clock)
{
    sf::Time shot;
    shot = clock.getElapsedTime();
    sf::Time reload = sf::milliseconds(1500);
    std::cout << shot.asMilliseconds();
    if(sf::Mouse::isButtonPressed(sf::Mouse::Left) and shot >= reload )
    {
        std::cout << "Sup m8" <<std::endl;
        clock.restart();
    }
}

Another option is for the "logic" class to have the sf::Clock as its (probably private) member, in which case it would have direct access to it (assuming "logic" is a class).

One other possibility is to make the clock static. It might work  :P
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything