Advertisement

delay in game loop

Started by April 08, 2002 06:42 PM
7 comments, last by hibiki_konzaki 22 years, 7 months ago
I need to know how to delay a loop so it will have a frame rate of 30 fps. I tried using the code in Andre Lamothe''s "Tha Black Art of 3-D Game Programming" but it didn''t work. Thanks.
HibikiWheres the any key?www.geocities.com/dragongames123/home.html
find your elementat mutedfaith.com.<º>
why would you want only 30 frames per second. usually you want to speed up a loop.

i will venture a wild guess and say that you want to delay it because if it goes faster than that your objects move too fast?

instead of doing:

position = position + 4;

do a physics equation:

position = velocity * timeStep + position;

timeStep is the amount of time that has passed since you last ran the loop.

then you set an items velocity and it will move across the screen at a constant rate no matter what the framerate

if i'm smoking crack and there's another reason, bitch slap me

-me

[edited by - Palidine on April 8, 2002 7:49:56 PM]
Advertisement
Well, it''s for an RPG battle system, and I want the enemy/monster you are fighting to be able to attack every 5 seconds (every loop a varible is incremented then if the variable = 150, the monster attacks) and the player to attack every 3 (basically the same thing as the monster)
HibikiWheres the any key?www.geocities.com/dragongames123/home.html
find your elementat mutedfaith.com.<º>
ok

well instead of timing your game by the loop you should time it by the computers clock.

so basically on each game loop you get the time and see how much time has passed since the last loop then decrement all your creatures timeTillNextAttack by that difference. when a creature''s timeTillNextAttack variable is <= 0 then it can attack on this round.

framerate is never going to be a constant thing so it''s hard to make it exact. also slowing down your game loop is a really bad habit to get into.

it''s waaaay easier to do get the time and see if each monster can attack yet.

i''d tell you how to get time, but then i don''t really know offhand how to do it.

try looking into some of the tutorials on this site. or just keep posting here and i''m sure someone else will have a specific answer on getting system time.

also it will help if you say what language and on what platform you are coding. also is it windows programming or command line text programming?

-me
I''d reccommend doing something like this:


  DWORD MonsterAttackTimer;/*...blah blah blah we skip down to where you start the instance of whatever monster*/MonsterAttackTimer = timeGetTime()+5000; //5000 miliseconds is 5 seconds but you probably knew that.// Blah blah blah down to battle loop:if (MonsterAttackTimer>timeGetTime()){MonsterAttackTimer = timeGetTime()+5000;Monster.Attack();  //Or whatever code you use}  


Hope that''s at least mildly helpful =D

-=Lohrno
IC, well I use C++ for a text based console game.
HibikiWheres the any key?www.geocities.com/dragongames123/home.html
find your elementat mutedfaith.com.<º>
Advertisement
Ahhhh..then that code will not work! =D But maybe you can at least get the idea! =D

Try using clock()? I don''t know if thats an ok Console command.

-=Lohrno
quote: Original post by hibiki_konzaki
IC, well I use C++ for a text based console game.

quote: Original post by Lohrno
Ahhhh..then that code will not work!

Not necessarily true. Console applications under Win32 still have access to the Win32 API, so just include <mmsystem.h>, add winmm.lib to the project and live happy. Timing is system-specific anyway, so you''ll need to do similar things on every platform.

Btw, the conditional should have been if( MonsterAttackTimer < timeGetTime() ) (note the less-than symbol!) and should have been placed in a loop (within which other timing conditions would be checked and other processing would proceed).

For a more advanced and more robust solution, register callbacks and intervals with your timing system and have it call the registered functions when appropriate.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions ]
Thanks to Kylotan for the idea!
Ahhhh yes right, oops! =D

-=Lohrno

This topic is closed to new replies.

Advertisement