Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Fri Dec 06, 2019 10:50 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: What's the Best Way to Pause a Program?
PostPosted: Tue Jun 02, 2009 5:06 am 
Offline

Joined: Fri Sep 30, 2005 1:53 pm
Posts: 914
Location: Philadelphia, PA
I don't mean an indefinite pause, as when you leave the room and want to resume a game after you "pause" and come back.

I mean a short (five-second?) pause (1) to provide the illusion that the computer is taking a moment to figure out its response and (2) to give the human user opportunity to read what is on the screen before it changes.

Example:

I'm finishing a game that allows four options: (1) computer vs. computer, (2) human vs. computer (i.e., human starts first), (3) computer vs. human (i.e., computer starts first), and (4) human vs. human. Suppose we have (pseudo-) code for the computer's move, using option (3):

StaticText1.Text = "I move my piece at " + Str(Row1) + ", " + Str(Col1) _
" to " + Str(Row2) + ", " + Str(Col2) + "."
ComputerMove Row1, Col1, Row2, Col2
StaticText1.Text = "Now it's your turn!"

Suppose further that I want to pause the computer for five seconds between these steps, something like this:

StaticText1.Text = "I move my piece at " + Str(Row1) + ", " + Str(Col1) _
" to " + Str(Row2) + ", " + Str(Col2) + "."
Pause (5)
ComputerMove Row1, Col1, Row2, Col2
Pause (5)
StaticText1.Text = "Now it's your turn!"

(It's not a very good example, but you get the point.)

What's the best way to pause the game for, say, five seconds?

Barry Traver

P.S. The question isn't theoretical. I'm almost finished with a 2D Sprite version of a game I call "NimRow." taken, I think, from Martin Gardner's "Mathematical Recreations" column in Scientific American, although I can't locate the particular column at the moment.


Top
 Profile  
Reply with quote  
 Post subject: Re: What's the Best Way to Pause a Program?
PostPosted: Tue Jun 02, 2009 5:14 am 
Offline

Joined: Tue Jan 03, 2006 10:38 am
Posts: 608
If you are having your code in a Thread, you can call Thread1.Sleep(5000)
Or place your code in a Timer's Action event, setup the timer to fire once in 5000 miliseconds, and once you want to start it, you enable it.

If you are not using Threads or Timers, you should start using them. By having code running in separate thread, your GUI will be fresh and won't freeze, while you have some calculations done behind. Timer's also run in a new thread. Timers are more easier to handle.

_________________
Rype :: book, do, charge, collect, track time, track expenses and invoice easily on the go.
Available on Mac, Windows, iPhone, Android, WindowsMobile.
http://www.rypenow.com


Top
 Profile  
Reply with quote  
 Post subject: Re: What's the Best Way to Pause a Program?
PostPosted: Tue Jun 02, 2009 11:48 am 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
Quote:
Timer's also run in a new thread.

They do not. Timers run in the main thread. The code in a timer can easily tie up the GUI if it takes too long.


Top
 Profile  
Reply with quote  
 Post subject: Re: What's the Best Way to Pause a Program?
PostPosted: Thu Jun 04, 2009 6:32 am 
Offline

Joined: Fri Sep 30, 2005 1:53 pm
Posts: 914
Location: Philadelphia, PA
Is there any reason why this "standard approach" should not be used?

The following is from Matt Neuberg's REALbasic: The Definitive Guide (2nd edition), pp. 548-549:

Quote:
You can use [Ticks and Microseconds], for example, to pause for a certain interval. You may recall that in chapter 5 these lines were used to pause a racing ProcessBar for some random amount of time:
max = rnd * 15 + 1
i = ticks
while ticks - i < max
wend
That's quite a standard approach for pausing. We define how many ticks we want to pause for. Then we learn the current Ticks value, but we don't really care what it is: we only want to keep checking the Ticks value, as the time goes by, until it has increased by the amount of time we want to pause for. Thus, we are guaranteed that we exit the While loop that much later than we entered it.

Since, according to the REALbasic Language Reference, a tick is a "60th of a second", the following, according to Neuberg, would be "quite a standard approach for pausing" for five seconds (unless things have changed since the second edition of his book was published, although the code seems to work with no problems and the current REALbasic Language Reference doesn't seem to advise against it):

i = ticks
while ticks - i < 5 * 60
wend

Any comments?

Barry Traver


Top
 Profile  
Reply with quote  
 Post subject: Re: What's the Best Way to Pause a Program?
PostPosted: Thu Jun 04, 2009 9:34 am 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
Quote:
Is there any reason why this "standard approach" should not be used?

I'm not sure I agree that that is "standard". It certainly is "old school", but there are a couple of problems with it. It can cause your app to appear to be locked up, and it burns cpu unnecessarily. The equivalent approach would be to sleep for that time period. It uses less cpu, but still locks the app, unless you're using threads. Bottom line, though, it's best not to do anything that will cause your app to appear "dead". "Thinking" should not be the same as "locked up".

Tim


Top
 Profile  
Reply with quote  
 Post subject: Re: What's the Best Way to Pause a Program?
PostPosted: Thu Jun 04, 2009 11:42 am 
Offline

Joined: Fri Sep 30, 2005 9:12 am
Posts: 725
Location: Rockford, IL
Couldn't you use App.SleepCurrentThread?

_________________
RB2010r2.1 Pro on Windows 7


Top
 Profile  
Reply with quote  
 Post subject: Re: What's the Best Way to Pause a Program?
PostPosted: Fri Jun 05, 2009 2:46 am 
Offline

Joined: Fri Sep 30, 2005 1:53 pm
Posts: 914
Location: Philadelphia, PA
Tim,

Good explanation!

"piMaster,"

Good question!

Everyone,

Another question: Would putting App.DoEvents inside the loop in Neuberg's code solve the problem(s) in his "old school" approach?

Barry Traver


Top
 Profile  
Reply with quote  
 Post subject: Re: What's the Best Way to Pause a Program?
PostPosted: Wed Jun 10, 2009 2:10 pm 
Offline
User avatar

Joined: Thu Mar 05, 2009 1:03 pm
Posts: 1786
Location: USA
App.DoEvents isn't usually recommended in GUI apps, IIRC...
Trust me on this: Threads are better for you & for the user's CPU... :)

_________________
With great power comes great responsibility.
Learn something new every day, and the rest will take care of itself.
Life is a journey, not a destination. Enjoy the trip! :D


Top
 Profile  
Reply with quote  
 Post subject: Re: What's the Best Way to Pause a Program?
PostPosted: Wed Jul 08, 2009 2:00 pm 
Offline
User avatar

Joined: Sat Oct 01, 2005 1:23 pm
Posts: 74
DoEvents is very dangerous (as is the equivalent in Visual Basic) since it lets the user do thing like click buttons that launch code like the code that's calling DoEvents (what, your code isn't re-entrant? have fun debugging).

Pausing a game is probably best done by explicitly handling game time (e.g. via threads) separately from real time and having objects behave based on game time. E.g. imagine you have an in-game "clock" which updates game time before and after every screen refresh. If it's paused, then game time stops updating.

There are MANY reasons to use such approach -- elegantly handling "pause" is merely one of them. Networked games need to be acutely aware of what "time" it is on the client, server, etc. Explicitly handling game time also lets you trivially implement stuff like "bullet time" or "fast forward" in strategy games (e.g. speed things up until something interesting happens). It also lets you (sensibly) keep the UI alive while the game is paused (e.g. in Baldur's Gate, et al, you can pause, and then scroll around the map and issue commands to your dudes).


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group