Vizard 7 » Reference » Flow control » Directors » Director basics

Director basics

The director command allows you to execute a function that includes interruptions without halting rendering of the world or the execution of other code within your script. The function is run as a parallel thread to the rest of the script's activity. If you quit the world before this function is executed, Vizard will force them to quit and warn you that it did so. Director functions are useful for performing IO operations that would otherwise halt the rendering of the world or the execution of other code (e.g. reading data from a large file, downloading a file from the Internet)

Note:  The director command creates an asynchronous thread and can cause unexpected results if not used correctly. In almost all cases, tasks should be used instead.

In the following example a director function is used to perform a file operation. A task is used to call the director function and wait for it's return:

import viz
import viztask


#Define a function that returns the last line of a text file
def getLast():
    file = open('data.txt', 'r')    
    for line in file:
        last = line

    return last 
def fileTask():
    yield viztask.waitKeyDown(' ')
    viz.logNotice('Started file operation')
    #Execute the getLast function in a separate thread
    #and wait for it to complete and return some data
    data = yield viztask.waitDirector(getLast)
    viz.logNotice('Finished file operation, last line is: ',data.returnValue)

viztask.schedule( fileTask() )

See also

In this section:

Director command table

Other sections:

Action basics

Tasks basics