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

Author Topic: I want to make sprites appear with a random time delay  (Read 14848 times)

0 Members and 1 Guest are viewing this topic.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: I want to make sprites appear with a random time delay
« Reply #30 on: March 15, 2016, 04:41:14 pm »
A way to avoid the missing the passing of time when the application is not active (i.e. blocked during OS modification), you can simple use an accumulation time and add to it the amount of time that has passed since the last frame - as long as that frame time is shorter than a chosen boundary.

sf::Clock;
sf::Time frameTime;
sf::Time accumulatedTime = sf::Time::Zero;
sf::Time maximumFrameTime = sf::milliseconds(200);

{
    // inside main loop
    frameTime = clock.restart();
    if (frameTime > maximumFrameTime)
        frameTime = maximumFrameTime;
    accumulatedTime += frameTime;
}

Then, "accumulatedTime" is the current time passed and would never process a frame of more than 200 milli-seconds. Note that some time can be discarded in this method. Although that is the intention for this method, it may be unwanted in some cases (real-time timing, for example).

Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: I want to make sprites appear with a random time delay
« Reply #31 on: March 16, 2016, 08:28:51 am »
... what do you recommend then?
Well, there are many ways to do it.
If you want to stick to your current solution you could modify it slightly as
 if ((time.asMilliseconds() >= t1.asMilliseconds()) &&
    (time.asMilliseconds() <= t2.asMilliseconds()) ||
    time.asMilliseconds() > t2.asMilliseconds()) {
that would address the specific problem I pointed out, but it's not really elegant.

Another option would be to calculate the time in the future when you want the object to spawn as "now() + nr_of_ms_until_spawn" and then simply check if "now()" is greater than or equal to that value and spawn an object if it is and then calculate a new spawn time.

What I would probably do myself would be to generate a random number between 500 and 1000 using the facillities in the <random> header (like std::random_device, std::mt19937 & std::uniform_int_distribution) and then set a timer to trigger a spawn_object() callback after that many milliseconds.

See also:
 http://en.cppreference.com/w/cpp/numeric/random
 http://en.cppreference.com/w/cpp/header/random
 http://www.bromeon.ch/libraries/thor/documentation/v2.0/classthor_1_1_callback_timer.html
« Last Edit: March 16, 2016, 08:31:02 am by Jesper Juhl »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: I want to make sprites appear with a random time delay
« Reply #32 on: March 16, 2016, 08:10:32 pm »
 if ((time.asMilliseconds() >= t1.asMilliseconds()) &&
    (time.asMilliseconds() <= t2.asMilliseconds()) ||
    time.asMilliseconds() > t2.asMilliseconds()) {
Assuming that t1 is before t2, that code is equivalent to:
if (time.asMilliseconds() >= t1.asMilliseconds()) {
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: I want to make sprites appear with a random time delay
« Reply #33 on: March 16, 2016, 09:23:33 pm »
Right you are Hapax.
I didn't think too much about that since I think the better option in any case is a timer with a random time (within the required limits) and a callback function is the better option. So I just tacked on a bit of code to his version to address the issue I noted without giving it much thought ;)

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: I want to make sprites appear with a random time delay
« Reply #34 on: March 16, 2016, 09:53:03 pm »
Just as an aside, there's no real need for us to use "asMilliseconds()" here as an sf::Time can be compared to another sf::Time.
That line could just be:
if (time >= t1) {
;D
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: I want to make sprites appear with a random time delay
« Reply #35 on: March 16, 2016, 09:55:00 pm »
Sure. But that's not really central to OP's problem. But you are right of course (and it is nicer that way).
« Last Edit: March 16, 2016, 09:56:34 pm by Jesper Juhl »

 

anything