WorldViz User Forum

WorldViz User Forum (https://forum.worldviz.com/index.php)
-   Vizard (https://forum.worldviz.com/forumdisplay.php?f=17)
-   -   return variable from keypress function (https://forum.worldviz.com/showthread.php?t=2453)

starbug 01-04-2010 12:43 PM

return variable from keypress function
 
I am relatively new to Vizard and Python, could you point me to some solutions for the following problem?

I want to change a variable in function MyTask() according to a keypress - input in function onKeydown() (if-switch). I just haven't found a way to return a variable from onKeydown. I tried the viz.callback function, and to give it enough time, I am trying to combine it with the viztask.waitKeyDown() function, but I just cannot produce an output form onKeydown.

Here the respective code:

def onKeydown(key):
if key == 'y':
incr = 5
elif key == 'n':
incr = -5
return incr

def MyTask():

while True:
yield viztask.waitKeyDown(('y','n'))
a = 10
b = onKeydown(viz.callback(viz.KEYDOWN_EVENT, onKeydown))
a = a + b
print a

viztask.schedule( MyTask() )

Any suggestions? thanks a lot!

Jeff 01-05-2010 09:09 AM

You can have a data object returned with waitKeyDown that will contain the key that was pressed and the time it occurred. You can then do your if statement based on that keypress data.
Code:

import viz
viz.go()

import viztask

def MyTask():
        d = viz.Data()
        while True:
                yield viztask.waitKeyDown(['y','n'],d)
                print 'Key '+d.key+' was pressed'
               
viztask.schedule( MyTask() )


starbug 01-07-2010 11:14 AM

Thanks Jeff,

the printing of variables created inside the function always worked for me, but not the returning. But I guess it comes down to 'generator functions', their special return - behavior, and that python is not matlab.
Have to learn a bit more about object oriented programming before my next post :o

Am using global variables that I change within functions now rather than trying to return something from a function, and that works alright.

thanks anyway,
starbug


All times are GMT -7. The time now is 03:00 AM.

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