WorldViz User Forum

WorldViz User Forum (https://forum.worldviz.com/index.php)
-   Vizard (https://forum.worldviz.com/forumdisplay.php?f=17)
-   -   Problem with viztask, callback, and oncollide (https://forum.worldviz.com/showthread.php?t=5689)

emmy rando 04-11-2016 01:02 PM

Problem with viztask, callback, and oncollide
 
Hello, I am trying to use viztask to wait for at least 5 seconds before calling a function as seen below. I know you need to call condition 1 with viztask.schedule(condition1()), but with that I still get errors maybe it's because of the second function in it which i am using the yield call on. But I have tried using it with the callback but to no avail. Please any help will be appreciate and the oncollide function is not called anywhere else but in the function condition1.

def condition1():
#Code...........
.
.
.
#more code
def oncollide(e):
#Check if a ball collided with a duck
#code.............
if e.obj2 in ducks:
#code.............
e.obj2.execute(2)
#code.............
#Increment score
scoreLabel.score += 10
scoreLabel.message(str(scoreLabel.score))
panel.visible(True)
endPanel.visible(False)
op1PanelLabel6.label.color(viz.GREEN)
yield.waitTime(5)
default()

elif e.obj2 in ducks2:
#code.............
e.obj2.execute(2)
#code.............
#Increment score
scoreLabel.score += 15
scoreLabel.message(str(scoreLabel.score))
panel.visible(True)
endPanel.visible(False)
op1PanelLabel6.label.color(viz.GREEN)
yield.waitTime(5)
default()

elif e.obj2 in ducks3:
#code.............
e.obj2.execute(2)
#code.............
#Increment score
scoreLabel.score +=20
scoreLabel.message(str(scoreLabel.score))
panel.visible(True)
endPanel.visible(False)
op1PanelLabel6.label.color(viz.GREEN)
yield.waitTime(5)
default()

elif e.obj2 not in ducks or e.obj2 not in ducks2 or e.obj2 not in ducks3 :
#code.............

#code.............
viz.callback(viz.COLLIDE_BEGIN_EVENT,oncollide)

Jeff 04-12-2016 07:23 AM

Can you describe the end result you are looking for in this script?

emmy rando 04-12-2016 08:03 AM

I am sorry, but the reason why I didn't include much code is because everything works. The problem is, In the oncollide function there's a yield to pause time for 5 secs before calling the function default(). But for I keep getting errors on the use viztask, maybe it's because the function called is within the fuction using viztask.schedule. If so how do I implement the use of yield.waitTime in a callback oncollide function.
def condition1():
#Code...........
.
.
.
#more code
def oncollide(e):
#Check if a ball collided with a duck
#code.............
if e.obj2 in ducks:
#code.............
e.obj2.execute(2)
#code.............
#Increment score
scoreLabel.score += 10
scoreLabel.message(str(scoreLabel.score))
panel.visible(True)
endPanel.visible(False)
op1PanelLabel6.label.color(viz.GREEN)
yield.waitTime(5)
default()

elif e.obj2 in ducks2:
#code.............
e.obj2.execute(2)
#code.............
#Increment score
scoreLabel.score += 15
scoreLabel.message(str(scoreLabel.score))
panel.visible(True)
endPanel.visible(False)
op1PanelLabel6.label.color(viz.GREEN)
yield.waitTime(5)
default()

elif e.obj2 in ducks3:
#code.............
e.obj2.execute(2)
#code.............
#Increment score
scoreLabel.score +=20
scoreLabel.message(str(scoreLabel.score))
panel.visible(True)
endPanel.visible(False)
op1PanelLabel6.label.color(viz.GREEN)
yield.waitTime(5)
default()

elif e.obj2 not in ducks or e.obj2 not in ducks2 or e.obj2 not in ducks3 :
#code.............

#code.............
viz.callback(viz.COLLIDE_BEGIN_EVENT,oncollide)

def Begin():
viztask.schedule(condition1())

Jeff 04-12-2016 10:39 PM

You can wait for a physics collision event within a task function. Here's an example:

Code:

import viz
import viztask

viz.go()

viz.clearcolor(viz.SLATE)

ground = viz.addChild('ground.osgb')
ground.collidePlane()

ball = viz.addChild('beachball.osgb',pos=[0,3,6])
ball.collideSphere()
ball.enable(viz.COLLIDE_NOTIFY)

def collideTask():
       
        # wait for spacebar to enable physics
        yield viztask.waitKeyDown(' ')
        viz.phys.enable()
       
        # wait for collision event
        d = yield viztask.waitEvent(viz.COLLIDE_BEGIN_EVENT)
       
        if d.data[0].obj1 == ball:
                collisionPoint = d.data[0].pos
                print 'the ball collided at',collisionPoint
       
        # wait 5 seconds
        yield viztask.waitTime(5)
        print '5 seconds elapsed'

viztask.schedule( collideTask() )

Or, from within the onCollide function, you could use vizact.ontimer2 to call default() after 5 seconds.

emmy rando 04-13-2016 06:47 AM

I tried to use the vizact.ontimer2, and it was the perfect solution my program needed. Thanks so much. Great help.


All times are GMT -7. The time now is 11:44 PM.

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