is there a wait command?

Understanding the language, error messages, etc.

is there a wait command?

Postby noah » Mon Jul 25, 2016 8:40 am

Hi,
Is there any way to make the unit pause for a specified amount of time before continuing? Something like the sleep function you can put in C++ (I understand that this is C++ but can you use other libraries?) or the wait __ function on Scratch.

I want to do something like this:
guy 1: don't do it!
[2 second pause]
guy 2: I'll do what I need to!
[2 second pause]
[continue with game, more plot blah, blah, etc]

I hope I've made what I mean clear.
Thanks in advance,
-Noah :)
User avatar
noah
 
Posts: 25
Joined: Mon Jul 25, 2016 8:32 am
Location: Spiffin' Britain

Re: is there a wait command?

Postby Sorunome » Mon Jul 25, 2016 9:06 am

Yeah, it's called delay() (i think), where the argument is the delay in milliseconds (i think, or microseconds >.> )
User avatar
Sorunome
 
Posts: 629
Joined: Sun Mar 01, 2015 1:58 pm

Re: is there a wait command?

Postby noah » Mon Jul 25, 2016 9:12 am

Yeah it is, thanks. I never worked out what micro and milliseconds actually accounted for in terms of a second, but delay(1000) make it pause for 1 second. I guess that's milliseconds?
User avatar
noah
 
Posts: 25
Joined: Mon Jul 25, 2016 8:32 am
Location: Spiffin' Britain

Re: is there a wait command?

Postby rodot » Mon Jul 25, 2016 11:50 am

You shouldn't use delay as it blocks everything happening in the background (in gb.update() actually) : sound, backlight, battery monitoring, buttons, etc, so you Gamebuino will be completely unresponsive for the duration of the delay. For example the user won't be able to skip the dialog or to get back to the menu.

The preferred way is to time events using gb.frameCount, which is incremented at each frame (each time gb.update() returns true). By default games run at 20 frames per second.
http://gamebuino.com/wiki/index.php?title=Gb.frameCount
User avatar
rodot
Site Admin
 
Posts: 1290
Joined: Mon Nov 19, 2012 11:54 pm
Location: France

Re: is there a wait command?

Postby noah » Mon Jul 25, 2016 1:08 pm

I was just going to say, everything seems to screw up when I used delay. So if I wanted a 2 second delay I should use gb.frameCount(40)?
User avatar
noah
 
Posts: 25
Joined: Mon Jul 25, 2016 8:32 am
Location: Spiffin' Britain

Re: is there a wait command?

Postby noah » Mon Jul 25, 2016 1:22 pm

well... that didn't work. I looked at the example but that shows a blink.
I tried
Code: Select all
void loop(){
  if(gb.update()){
    if(gb.frameCount < 40){
      blah blah
    }else if(gb.frameCount < 80){
      blah blah
    }
  }
}

...but this didn't work, I'm assuming because I stayed more than two seconds on the title screen. What is the best way of implementing this, I just wanted a wait?! :D
User avatar
noah
 
Posts: 25
Joined: Mon Jul 25, 2016 8:32 am
Location: Spiffin' Britain

Re: is there a wait command?

Postby Sorunome » Mon Jul 25, 2016 1:35 pm

rodot wrote:You shouldn't use delay as it blocks everything happening in the background (in gb.update() actually) : sound, backlight, battery monitoring, buttons, etc, so you Gamebuino will be completely unresponsive for the duration of the delay. For example the user won't be able to skip the dialog or to get back to the menu.

[...]

Ah, right, I noticed that during writing of the engine of the RPG project thing. However, I couldn't use the other function stuff because of __reasons__ so I wrote a thing where it updates sound inside the interrupt, i already ran custom button code and it wouldn't be too noticable for backlight / battery monitoring to not be updated in a bit.

The all-interrupt sound thing can be found here, in case you are interested

noah wrote:Yeah it is, thanks. I never worked out what micro and milliseconds actually accounted for in terms of a second, but delay(1000) make it pause for 1 second. I guess that's milliseconds?

Micro and milli are actually super easy! (Thanks metric system!)
Micro --> 10^(-6)
Milli --> 10^(-3)

Or, like, if you have 12345 milliseconds that's the same as 12.345 seconds (just move the comma left by three)
If you have 12345 microseconds that's the same as 0.012345 seconds (just move the comma left by six)
Calculations in the other direction are just about moving the coma into the other direction ^.^

noah wrote:well... that didn't work. I looked at the example but that shows a blink.
I tried
Code: Select all
void loop(){
  if(gb.update()){
    if(gb.frameCount < 40){
      blah blah
    }else if(gb.frameCount < 80){
      blah blah
    }
  }
}

...but this didn't work, I'm assuming because I stayed more than two seconds on the title screen. What is the best way of implementing this, I just wanted a wait?! :D

The thing is that gb.frameCount returns the current frame count which is keep on increasing since the beginning of tiiiiime! (the program)
Here's a little example, which is untested, though:
Code: Select all
void wait(byte frames){
  gb.frameCount = 0; // reset the framecount
  while(gb.frameCount < frames){ // repeat the next until the framecount is equal to how many frames we want to wait
    while(!gb.update()); // wait until things need updating
    // right here you would put things to update the screen, if it got erased, that is (configurable via gb.display.persistence )
  }
}

That would however destroy the frame count, for without destroying something like this might work:
Code: Select all
void wait(int frames){
  frames += gb.frameCount;
  while(gb.frameCount < frames){
    while(!gb.update());
  }
}
However I don't know how well that handles with int-overflows
Last edited by Sorunome on Mon Jul 25, 2016 1:47 pm, edited 4 times in total.
User avatar
Sorunome
 
Posts: 629
Joined: Sun Mar 01, 2015 1:58 pm

Re: is there a wait command?

Postby noah » Mon Jul 25, 2016 1:41 pm

@Sorunome on an unrelated note, I first heard of The Game two years ago and had forgotten about it since then. Now I keep losing. Thanks. ;)
User avatar
noah
 
Posts: 25
Joined: Mon Jul 25, 2016 8:32 am
Location: Spiffin' Britain

Re: is there a wait command?

Postby Sorunome » Mon Jul 25, 2016 1:45 pm

I edited in code examples in my previous post, not sure if you saw them (I thought nobody was online right now anyways :P )
User avatar
Sorunome
 
Posts: 629
Joined: Sun Mar 01, 2015 1:58 pm

Re: is there a wait command?

Postby noah » Mon Jul 25, 2016 1:52 pm

Thanks for the milli/micro thing! That really helps.

Awesome! I just used the first one, and it works. But does resetting the frame count ruin anything? At the moment I can't think of any reason why it might be a problem... but would it be better to use the other one?

Thanks, noah :)
User avatar
noah
 
Posts: 25
Joined: Mon Jul 25, 2016 8:32 am
Location: Spiffin' Britain

Next

Return to Programming Questions

Who is online

Users browsing this forum: No registered users and 16 guests