Correct timing is a crucial issue for every good script that should be able to run on many different systems. As Hollywood is available for a multitude of platforms you have to think about script timing if you plan to give your script to others. The basic problem is that if you do not add speed limiters to your script, it will run as fast as possible. That might not be a problem on old 200 Mhz systems but on a gigahertz machine it surely is a problem. Imagine you have a game and use the following loop:
/* Bad code */ While quit = False dir = p_QueryInput() If dir = "Left" Then p_MoveSpriteLeft() If dir = "Right" Then p_MoveSpriteRight() .... p_RedrawDisplay() Wend |
This loop has two serious problems:
The solution to the problem is pretty simple: We just need to tell Hollywood to execute this loop only a certain amount of times per second. For most games, it is completely sufficient to query for input and draw graphics 25 times per second. There are two methods how you can implement such a throttle:
/* Good code */ StartTimer(1) ; start timer #1 While quit = False dir = p_QueryInput() If dir = "Left" Then p_MoveSpriteLeft() If dir = "Right" Then p_MoveSpriteRight() .... p_RedrawDisplay() WaitTimer(1, 40) ; do not run faster than 40 milliseconds Wend |
Now our loop will never run faster than 40 milliseconds. Thus, it will never be executed more than 25 times per second because 25 * 40 = 1000. Hence, a game using this loop will run at the same speed on every system - no matter if the CPU has 50mhz or 1ghz.
/* Good code */ Function p_MainLoop() ; this does the same code as our While-Wend loop above dir = QueryInput() If dir = "Left" Then MoveSpriteLeft() If dir = "Right" Then MoveSpriteRight() .... RedrawDisplay() EndFunction ; call MainLoop() 25 times a second -> 40 * 25 = 1000 milliseconds SetInterval(1, p_MainLoop, 40) While quit = False WaitEvent Wend |
This code does the same as the code above using WaitTimer(). The only difference is that you have to use WaitEvent() with SetInterval() because interval functions trigger Hollywood events.
Both of the methods discussed above are easy to use and efficient. It is up to you to decide which one you prefer.