TOO fast! How to slow execution.

I've written a game. When the game executes it can process literally thousands of "steps" in just seconds.

What's the best approach for slowing execution down such that the game "speed" will be about the same no matter CPU speed?

Thanks
LVL 5
volkingAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
PaulHewsConnect With a Mentor Commented:
I think reducing the 8 to a single timer may be an even more important factor.  What may be happening is that one timer is triggering a graphics call before the last one has finished its work.  Thus reduce the code path to a single timer and you shouldn't have that problem.
0
 
Razzie_Commented:
Well you can halt program execution by using:

Thread.Sleep(n);

where 'n' is the number of ms to halt execution. So Thread.Sleep(1000); would pause the program for 1 second. The Thread class is in the System.Threading namespace.

HTH,

Razzie
0
 
PaulHewsCommented:
Use a timer...  You can use System.Windows.Forms.Timer or System.Timers.Timer classes.  Both fire an event a defined intervals.  

Using Sleep is not a great idea.  Your program is not responsive when the thread is sleeping.

Some things don't need to be regulated by time.  It wouldn't make much sense to limit your program's AI (if it has any) by regulating it through a timer.  However, video games with animation should be timer event driven so that the action appears the same on all systems.  The System.Timers.Timer will be slightly more accurate than the winforms version, because it's events are not processed through the form's message queue.

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Razzie_Commented:
My bad, PaulHews is absolutely right, missed the fact it was a game, thought you just wanted to halt program execution for x seconds.

Go with the Timer! :)
0
 
volkingAuthor Commented:
@ PaulHews
First, this is a Video Game with many separate things happening.

Second, took your advice and tried a System.Windows.Forms.Timer (needed 8) ... works great ...... BUT ........ hmmmmmmm ...... Now I have a gremlin! ...

Once in awhile (very very seldom with no apparent pattern) the program generates a "Generic GDI+" error. When I look close at the error message (a) I really don't understand what I'm reading and (b) it "appears" a GDI+ call was interupted by a Clock-Tic event. (If I click "Continue" and ignore problem, the program "appears" to continue along happily)

What do you think? How to fix?

a) capture error in code (HOW???) auto ignore and continue program
b) Change to System.Timers.Timer class (Will that solve the problem?)
c) some other advice .........???
0
 
volkingAuthor Commented:
@ PaulHews - My first step will be to collapse 8 separate timers into ONE "fine" timer, keep track of 8 timing-schemes (inside the one timer) and fire proper action(s) as needed. I'll let you know if this helps with Gremlin.
0
 
PaulHewsCommented:
You trap exceptions by wrapping the code in a Try ... Catch block.  How you deal with the error depends.  Something like this:

try
{
   ...   // code that causes error
}
catch (Exception ex)
{
  debug.writeline(ex.tostring);
}

Will at least write the full errror and stack trace to the output window... Useful because you can post the exception here, and analyse it further.  It also won't halt execution, however that may not be a good thing.  Unhandled exceptions caused by problematic graphics code can cause memory leaks.

It might be useful to try the other system timer.  As noted before, it isn't handled by the form's event processing, so that may be an advantage.  Hard for me to say without knowing what your code is actually doing.

>@ PaulHews - My first step will be to collapse 8 separate timers into ONE "fine" timer, keep track of 8 timing-schemes (inside the one timer) and fire proper action(s) as needed. I'll let you know if this helps with Gremlin.

Ouch, yes.  You definitely don't want 8 separate system timers making graphics calls.  Far better to handle it with state variables within a single timer event...
0
 
Razzie_Commented:
volking: you should not use the System.Windows.Forms.Timer because that is NOT an accurate timer by all means. It is best to use a System.Timers.Timer or a System.Threading.Timer.

I think switching to another timer will also get rid of the GDI+ error.
So:

a) try-catch
b) yes
0
 
volkingAuthor Commented:
Timers are not my Forte ... I don't have a "vision" of how they work.

Here is full project souce code (348kb)
http://www.qpop.com/rsp.zip

Extract to   C:\
Will create C:\RokSizPap\.......
Use Visual Studioi & open:  C:\RokSizPap\RokSizPap.sln
Run
Ignore everything on first screen (don't work yet) but press "GAME" button
Game window opens ... screen bottom press button "Phase 1 play"
Be patient ... takes a few rounds to trigger elimination ... first place I'm using Timer.

P.S. Got timer from www.TheCodeProject.com - Discovered can only use One-At-A-Time ... they interfere with each other!!!!!

Can you help?
0
 
volkingAuthor Commented:
Phone # in profile
Only check email every couple of hours (using dial-up)
0
 
PaulHewsCommented:
It's against the member agreement to solve problems by email or phone...And I don't have full VS 2003, I've only got VS 2002, and VB.NET for 2003, so no version of C# that can open that project.  I'll browse the code, but I can't guarantee I'll be able to help much further than I have.

0
 
PaulHewsCommented:
volking, thanks for the points.  Were you able to get it working with your timer?  
0
 
volkingAuthor Commented:
@ PaulHews - Nope ... discarded timer from TheCodeProject. Rebuilt from scratch using System.Timers.Timer.
class = TimeCentral
Any procedure anywhere subscribes to TimeCentral.NotifyMeList event
Any procedure anywhere calls TimeCentral.SetTimePoint((notify me every X milliseconds), (until a total of Y milliseconds have passed), (myPostMark is ABC))
One central timer object allows multiple event-subscriptions ...
... where returned event args identify which requestor asked to be notified.
... thus each requestor simply ignores any event not "post-marked" for it.

At first crack .... it seems to be working fine .....

After working with it awhile longer ... I will put it in TheCodeProject for everyone.

Later ...
0
 
PaulHewsCommented:
Glad to hear it is working out then.  :)
0
All Courses

From novice to tech pro — start learning today.