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 15344 times)

0 Members and 2 Guests are viewing this topic.

korczurekk

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • Email
Re: AW: Re: AW: I want to make sprites appear with a random time delay
« Reply #15 on: March 12, 2016, 10:19:45 pm »
clear() isn't always needed, well, it's useless in almost every bigger project which overwrites whole screen with some background
This is wrong. SFML requires you to call clear, due to double buffering.
I don't use it for a long time and haven't noticed any problems, what can be wrong?

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: AW: I want to make sprites appear with a random time delay
« Reply #16 on: March 12, 2016, 10:22:11 pm »
So you removed all clear statements?

You should always clear(), draw(), display().
clear() isn't always needed, well, it's useless in almost every bigger project which overwrites whole screen with some background.
Please, please don't say that.
Yes, if you do draw the entire screen, then clear() is pointless. But it is also harmless. It doesn't hurt performance and it doesn't screw up rendering.
When it /is/ needed it really is needed. You get strange rendering artifacts if you leave it out.
You should just always do it. It doesn't hurt when not needed and it does real needed work when needed (most of the time).
Please don't advice people to skip it.
clear(), draw(), display() - always!
« Last Edit: March 12, 2016, 10:24:49 pm by Jesper Juhl »

korczurekk

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • Email
Re: I want to make sprites appear with a random time delay
« Reply #17 on: March 12, 2016, 10:31:00 pm »
So you removed all clear statements?

You should always clear(), draw(), display().
clear() isn't always needed, well, it's useless in almost every bigger project which overwrites whole screen with some background.
Please, please don't say that.
Yes, if you do draw the entire screen, then clear() is pointless. But it is also harmless. It doesn't hurt performance and it doesn't screw up rendering.
When it /is/ needed it really is needed. You get strange rendering artifacts if you leave it out.
You should just always do it. It doesn't hurt when not needed and it does real needed work when needed (most of the time).
Please don't advice people to skip it.
clear(), draw(), display() - always!
I don't say people should skip it.  :)

However, those "strange rendering artifacts" are very characteristic and removing clear() helped me a lot in searching for other rendering bugs. I don't see any problem in using it for debugging.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: I want to make sprites appear with a random time delay
« Reply #18 on: March 12, 2016, 10:39:43 pm »
Quote
This is wrong. SFML requires you to call clear, due to double buffering.
clear() just fills the whole buffer with a color. If the next thing you do is to fill it with something else, it is indeed not required -- it is even mentioned in the tutorials ;)
And double-buffering has nothing to do with this.
Laurent Gomila - SFML developer

ikarah

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: I want to make sprites appear with a random time delay
« Reply #19 on: March 13, 2016, 01:58:45 am »
Alright back on topic haha. The random and all worked great. I want to be able to delay with time though for example

if (bla = bla){

Dosomething;

Wait 5 seconds then start again from the top
}

How can I do that using time or clock?

ikarah

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: I want to make sprites appear with a random time delay
« Reply #20 on: March 13, 2016, 07:02:10 pm »
Bump anyone?

OualidH38

  • Newbie
  • *
  • Posts: 46
    • View Profile
    • Email
Re: I want to make sprites appear with a random time delay
« Reply #21 on: March 13, 2016, 08:15:01 pm »
/*Use a boolean for your condition: mValue
a float that will holds the time: mCountDown
another float with a const value sf::seconds(5.f): TimeInterval

Use this inside an update function with an sf::Time dt that holds elapsedTime */


if (mValue && mCountDown <= sf::Time::Zero) {

                mValue= false;

                //Do your stuff here

                mCountDown += TimeInterval;
               
               
        }
        else if (mCountDown > sf::Time::Zero) {

                mCountDown -= dt;

}

 
« Last Edit: March 14, 2016, 12:14:37 am by OualidH38 »

ikarah

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: I want to make sprites appear with a random time delay
« Reply #22 on: March 13, 2016, 08:42:58 pm »
I don't really understand the code you sent me ?
Why is mvalue which is a bool less than time zero? What are you achieving with the += and the -= Please elaborate ..

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: I want to make sprites appear with a random time delay
« Reply #23 on: March 13, 2016, 08:58:52 pm »
Have you ever sat down and thought it through on your own? I mean programming shouldn't consist of waiting for people to suggest random things that one can then pretty much copy&paste.

I suggest to take a piece of paper and a pen, sit down and go step by step through the logic of how to go about it.
Make use of what you have (clock, random number generator, sprites) and figure out when you can use what where and how it will fit into your solution. ;)
« Last Edit: March 14, 2016, 01:17:20 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

OualidH38

  • Newbie
  • *
  • Posts: 46
    • View Profile
    • Email
Re: I want to make sprites appear with a random time delay
« Reply #24 on: March 14, 2016, 12:13:42 am »
I don't really understand the code you sent me ?
Why is mvalue which is a bool less than time zero? What are you achieving with the += and the -= Please elaborate ..

I suggest you to read carefully what I wrote, I'm not trying to compare a bool with sf::Time::Zero.
The if statement tests the boolean and the mCountDown value.

eXpl0it3r is right, you should learn a bit more before asking for a pre-made solution.

ikarah

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: I want to make sprites appear with a random time delay
« Reply #25 on: March 14, 2016, 11:30:21 am »
Actually I was able to solve my problem in a completely different way on my own which is fine. But, because I want to learn more and have a thirst for knowledge I am asking since I tried to understand the logic behind the code you put here and didn't really get the Boolean part? That's why I am asking. It's fine to ask questions when you don't understand otherwise you won't learn. As for asking for code to copy paste in that's obviously not what I am asking for because that is pointless and each and every program is written in a different way.

OualidH38

  • Newbie
  • *
  • Posts: 46
    • View Profile
    • Email
Re: I want to make sprites appear with a random time delay
« Reply #26 on: March 14, 2016, 01:51:31 pm »
Can you please past the solution here and put your problem as "solved" if you found a solution by your own?

I was not trying to be mean when I said that you should learn a bit more. The if statement was a basic condition, and you did not understand it. No offense.

I admire the fact that you are motivated and looking for how things work, but start with the begining (c++ basis, logic, etc) before.

It's fine when you ask questions of course, but the logic that I've put before is easy to understand.

Sorry if I offended you, that was not my purpose.
Good luck

ikarah

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: I want to make sprites appear with a random time delay
« Reply #27 on: March 14, 2016, 07:02:50 pm »
Well the way I did it was like so

sf::Time t1 = sf::milliseconds(500);
      sf::Time t2 = sf::milliseconds(1000);
      time = clock.getElapsedTime();
      sf::Event Event;
      if ((time.asMilliseconds() >= t1.asMilliseconds()) && (time.asMilliseconds() <= t2.asMilliseconds())) {
      clock.restart();
}

Basically that way it will meet this condition only once then the clock will restart achieving the delay effect I wanted. I understand if statements but, the way you went about it is just different.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: I want to make sprites appear with a random time delay
« Reply #28 on: March 14, 2016, 07:30:25 pm »
This is fragile and will fail if, for some reason, you are not passing through this code at least once every 0.5 seconds and you end up with time.asMilliseconds() returns 5000 for example.
This could happen if the user drags the window so events are not processed for a while or for many other reasons.
« Last Edit: March 14, 2016, 07:40:26 pm by Jesper Juhl »

ikarah

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: I want to make sprites appear with a random time delay
« Reply #29 on: March 14, 2016, 09:00:26 pm »
Thank you Jesper what do you recommend then?