WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 04-28-2008, 12:28 PM
Gladsomebeast Gladsomebeast is offline
Member
 
Join Date: Mar 2005
Location: Isla Vizta, CA
Posts: 397
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))
First, the code imports the viztask module of Vizard that provides the Task functionality. After adding our stoplight ball we define our task function just like a normal function. The function becomes a task function because of the "yield" statement. The yield statement indicates when the task pauses and waits for some seconds. When the wait time is up, the next lines of code are run until we hit another yield statement. The last line passes an instance of our "doStoplightTask" to the scheduler. The scheduler triggers the execution of the task. Tasks must be scheduled to run, they can't be called like normal functions.

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
More information regarding Vizard task is in your Vizard help file and at this address:
http://www.worldviz.com/vizhelp/VizTask_basics.htm
__________________
Paul Elliott
WorldViz LLC
Reply With Quote
 

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

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


All times are GMT -7. The time now is 10:18 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2022, vBulletin Solutions, Inc.
Copyright 2002-2018 WorldViz LLC