|
#1
|
|||
|
|||
Vizard Tip of the Month: Use Tasks to Simplify your Code
Vizard's Task feature makes writing code that you want to happen over time easier. Tasks are functions that pause as they wait for some condition (like 2 seconds to pass or spacebar pressed) before continuing with their local variables preserved.
Say you want a traffic light that changes color every second. Using timers, you could create a Vizard timer event that sets the next color based on a global variable that remembers the previous color. Or you could create a self-contained task function. Code:
import viztask ball = viz.add('white_ball.wrl', pos=[0, 1.7, 1]) def doStoplight(theBall): while True: theBall.color(viz.GREEN) yield viztask.waitTime(1) theBall.color(viz.YELLOW) yield viztask.waitTime(.5) theBall.color(viz.RED) yield viztask.waitTime(1) viztask.schedule(doStoplight(ball)) Creating tasks that wait for other tasks can simplify programs that progress though a series of states. Research experiments often progress though these states: get parameters; present a number of trials; finalize data recording. Here is a experiment framework using tasks: Code:
import viztask def doExperimentTask(): NUMBER_OF_TRIALS = 3 for i in range(NUMBER_OF_TRIALS): yield doTrialTask(i) print 'experiment done' viztask.schedule(doExperimentTask()) def doTrialTask(trialNumber): #present stimulus yield viztask.waitKeyDown(' ') #gather response #record data print 'trial number', trialNumber http://www.worldviz.com/vizhelp/VizTask_basics.htm
__________________
Paul Elliott WorldViz LLC |
#2
|
|||
|
|||
Viztask
Is it possible to use viztask in order to delay onCollide events? - i.e. to create the time interval after the collision when objects do not produce collision event?
|
#3
|
|||
|
|||
A tip of the month is an excellent idea. Thanks!
|
#4
|
|||
|
|||
Andrey,
Tasks can wait for a number of conditions, including events like COLLIDE_BEGIN_EVENT. Perhaps you want to do something like this? Code:
import viztask def doSomethingWhenCollidingTask(): while True: yield viztask.waitEvent(viz.COLLIDE_BEGIN_EVENT) print 'collidy !!!' #do something #........ yield viztask.waitTime(2) #ignore events for 2 seconds viztask.schedule(doSomethingWhenCollidingTask())
__________________
Paul Elliott WorldViz LLC |
#5
|
|||
|
|||
Thank you for sharing. I'm using director for task scheduling... Is there any different?
|
#6
|
|||
|
|||
Unless you are performing blocking operations (i.e. reading from file or socket) I strongly recommend against using director functions. Director functions use real operating system threads, therefore they are not deterministic and you could experience synchronization problems.
|
Thread Tools | |
Display Modes | Rate This Thread |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Vizard and Augmented Reality | realvision | Vizard | 4 | 04-04-2008 10:59 AM |
Vizard won't run | wouters | Vizard | 5 | 02-05-2008 11:12 AM |
Vizard 3.0 beta activation code | vnacooper | Vizard | 3 | 09-21-2006 12:03 PM |
wxPython with Vizard | farshizzo | Vizard | 18 | 09-29-2005 08:49 AM |
Vizard Crashes: causes are hard to determine, possible problem with the viz code | vr_boyko | Vizard | 1 | 01-07-2005 10:52 AM |