15.3 Script timing

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:

  1. There is no timing in this loop. The loop will always run as fast as the CPU of the host system allows. This is really bad. It means that the timing will only be correct on your system and nowhere else.

  2. This loop will eat 100% of your CPU power because there is no limit that says "Execute this loop 25 times a second and that's enough!". Consuming 100% of the CPU power might not be a problem on very slow systems but when running the script on a newer system the OS could noticeably slow down and the CPU fan might start up because of the heat generated by the CPU. Additionally, remember that Hollywood runs in a multitasking environment. In such an environment you have to be a good citizen and only take as much CPU time as you really need. If you take all the CPU time without really needing it, all other processes will get less CPU time.

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:

  1. Using WaitTimer(). This function accepts a timer and a timeout value. It pauses the script until the specified time has passed. Then the timer is reset and you can use it again. Our loop from above would look like the following using WaitTimer():

     
    /* 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.

  2. The second method is SetInterval(). This function allows you to install an interval function that Hollywood will call at the frequency you specify. Thus, you can tell Hollywood to call your game loop 25 times a second. The code to do this looks like this:

     
    /* 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.


Show TOC