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

Author Topic: [SOLVED]Having lots of trouble with sf::Clock and sf::Time  (Read 3010 times)

0 Members and 1 Guest are viewing this topic.

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
[SOLVED]Having lots of trouble with sf::Clock and sf::Time
« on: December 01, 2012, 03:35:11 pm »
I'm trying to measure a certain amount of time to be used by a recoil method with doesn't allow a gun to shoot again until the time is past. When I try to do this, it prints out odd numbers to the console and doesn't work at all.

bool Gun::recoil(float time)
{
    sf::Clock recoilTimeClock;
    sf::Time Timer;

    Timer = recoilTimeClock.getElapsedTime();

    std::cout << Timer.asSeconds() << std::endl;

    recoilTimeClock.restart();
}

When I do this, it prints out extremely weird numbers that don't correlate with the program in any way. A picture is attached below.

[attachment deleted by admin]
« Last Edit: December 02, 2012, 02:36:48 pm by The Terminator »
Current Projects:
Technoport

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Having lots of trouble with sf::Clock and sf::Time
« Reply #1 on: December 01, 2012, 03:38:38 pm »
The numbers are not weird, and they correlate perfectly with the program.

They are that small because you create a clock and immediately measure the elapsed time. recoilTimeClock should be a member variable.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
Re: Having lots of trouble with sf::Clock and sf::Time
« Reply #2 on: December 01, 2012, 03:43:06 pm »
5e-006 is the scientific way of writing numbers, 5 times 10 with exponent -6 or 5*10^(-6) or 0.000005. ;)
So everything is fine and works as it should. You could use
std::cout << std::fixed << Timer.sasSeconds() << std::endl;
to get the decimal representation.

As for your gun I'm not sure what exactly you're trying to do. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Re: Having lots of trouble with sf::Clock and sf::Time
« Reply #3 on: December 02, 2012, 03:09:24 am »
I'm extremely confused on how to measure this. What I pretty much want is if the user shoots, it triggers the method recoil which will not allow the user to shoot in a certain amount of time. Here's the code I have so far, but it doesn't work.

void Gun::shoot()
{
    if (currentBullets > 0)
    {
        if (recoil(recoilTime))
        {
            std::cout << "Reloading..." << std::endl;
            std::cout << std::fixed << recoilTime << std::endl;
            std::cout << recoil(recoilTime) << std::endl;
        }

        if (!recoil(recoilTime))
        {
            switch (gunType)
            {
                case UNKNOWN:
                    std::cout << "GunType = Unknown" << std::endl;

                    break;

                case PISTOL:
                    std::cout << "Shooting Weapon: Pistol" << std::endl;
                    currentBullets--;
                    std::cout << currentBullets << " bullets left!" << std::endl;

                    break;
            }
        }
    }

    else
    {
        std::cout << "Out of bullets!" << std::endl;
    }
}

bool Gun::recoil(float time)
{
    if (recoilTimeClock.restart().asSeconds() < time)
        return true;
    else
        return false;
}
Current Projects:
Technoport

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: Having lots of trouble with sf::Clock and sf::Time
« Reply #4 on: December 02, 2012, 06:49:51 am »
recoil is resetting the time elapsed every time you call it.  That's probably not the behavior you want.


ichineko

  • Newbie
  • *
  • Posts: 44
    • View Profile
Re: Having lots of trouble with sf::Clock and sf::Time
« Reply #5 on: December 02, 2012, 07:11:50 am »
http://www.sfml-dev.org/documentation/2.0/classsf_1_1Clock.php#a123e2627f2943e5ecaa1db0c7df3231b

restart does 2 things.  It resets the internal counter to 0, and then returns the amount of time that has elapsed since your game started, which means your check is always going to return true until the total time that your game has been open exceeds your time variable (which I assume doesn't change), and then it will always return false.;

Look into:
http://www.sfml-dev.org/documentation/2.0/classsf_1_1Clock.php#a799feb6acb099b57b58d8d20984fce11

What you want to do, is when you shoot successfully, restart the clock, and then in your recoil function, check the elapsed time.

clock.getElapsedTime().AsSeconds() < time



The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Re: Having lots of trouble with sf::Clock and sf::Time
« Reply #6 on: December 02, 2012, 02:36:29 pm »
http://www.sfml-dev.org/documentation/2.0/classsf_1_1Clock.php#a123e2627f2943e5ecaa1db0c7df3231b

restart does 2 things.  It resets the internal counter to 0, and then returns the amount of time that has elapsed since your game started, which means your check is always going to return true until the total time that your game has been open exceeds your time variable (which I assume doesn't change), and then it will always return false.;

Look into:
http://www.sfml-dev.org/documentation/2.0/classsf_1_1Clock.php#a799feb6acb099b57b58d8d20984fce11

What you want to do, is when you shoot successfully, restart the clock, and then in your recoil function, check the elapsed time.

clock.getElapsedTime().AsSeconds() < time

THANK YOU SO MUCH! I got it working perfectly and it's exactly what I want. Thanks once again!!  ;D ;D ;D
Current Projects:
Technoport