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

Author Topic: Populate vector every few seconds  (Read 1602 times)

0 Members and 1 Guest are viewing this topic.

JasonMcG

  • Newbie
  • *
  • Posts: 10
    • View Profile
Populate vector every few seconds
« on: September 20, 2017, 11:44:24 am »
Hi,

I have a Rock class with rock constructor that sets the initial position of the rock.
There should be multiple rocks in the game that appear periodically.

I created a vector,
Code: [Select]
vector<*Rock> _rock;
I want to push_back a new rock every few seconds. How do I do this?
I Tried:
Code: [Select]
sf::Clock clock;
int x = 5;
if (clock.getElapsedTime().asSeconds() == x) {
   _asteroids.push_back(new Asteroid(*_player));
}
x = x +5;

This does not work. Am I on the correct track? What should I do?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Populate vector every few seconds
« Reply #1 on: September 20, 2017, 12:17:13 pm »
Did you ask yourself why it doesn't work?
What's the chance of the clock being exactly 5 the moment you check it? Pretty much zero chance. Besides that floats should never be compared directly as their inaccuracy can lead to false results.

For your situation you can simply use "greater than or equal" and inside the if block you reset the clock.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

JasonMcG

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Populate vector every few seconds
« Reply #2 on: September 20, 2017, 12:18:39 pm »
I have tried that as well. Giving a small range, 4.5 - 5.5.
Should this work?
Code: [Select]
if(clock.getElapsedTime().asSeconds() > x-0.5 && clock.getElapsedTime().asSeconds() < x+0.5)
« Last Edit: September 20, 2017, 12:25:28 pm by JasonMcG »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Populate vector every few seconds
« Reply #3 on: September 20, 2017, 01:13:29 pm »
What happens when the time elapsed is 5.5 seconds? 5.6 seconds? It gets skipped. Don't forget that the clock continues until you restart it (as mentioned above) so even if this if statement caught it, it would probably only manage that once.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Tigre Pablito

  • Full Member
  • ***
  • Posts: 226
    • View Profile
    • Email
Re: Populate vector every few seconds
« Reply #4 on: October 03, 2017, 12:28:55 am »
Hi Jason

Why do you complicate using clock instead of a 'counter' variable? And why a constant interval to add the asteroids to the vector?

I have never used a clock in my poor SFML projects, nor know if they exist in SFML.Net

I give you a suggestion, you can take it or not

DateTime time = DateTime.Now;
rand = new Random();
int waiter = 0;
while (true)   // game loop
{
    time = time.AddMilliseconds(20);  // that is 50FPS

    if (waiter > 0)
        waiter--;
    else if (waiter == 0)
    {
        asteroids.Add(new Asteroid( .... ));
        waiter = rand.Next(50, 150);  // or whatever you want
    }

    DrawAsteroids();   // whatever it is implemented

    while (DateTime.Now < time)
        ;     // wait for the 20ms
}
 

Hope this helps. Feel free to ask whatever you need.
PS: Maybe someone could translate to C++?

 

anything