PDA

View Full Version : custom events within classes


Penguin
05-12-2009, 11:48 AM
Hi, is it possible to register more than one function for listing on a custom event? When pressing the 'e'-button the following code should print out 'B' and 'C'. But only 'C' is printed out. When I swap the two *.enableEvent(EVENT) lines then 'B' is printed out.

Therefore I guess only one function can listen to an event at the same time. Am I right?


import viz

class A(object):
def __init__(self):
pass

def enableEvent(self, EventID):
viz.callback(EventID,self.doit)

def doit(self):
pass

class B(A):
def __init__(self):
pass

def doit(self):
print "B"

class C(A):
def __init__(self):
pass

def doit(self):
print "C"

viz.go()

EVENT = viz.getEventID('EVENT')

b = B()
b.enableEvent(EVENT)
c = C()
c.enableEvent(EVENT)


def sendEvent():
viz.sendEvent(EVENT)

vizact.onkeydown('e',sendEvent)

farshizzo
05-12-2009, 12:04 PM
Yes, have a look in the documentation under the Reference -> Events -> Event basics section. It contains sample code showing how to use the viz.EventClass for registering multiple objects with an event.

Gladsomebeast
05-12-2009, 12:09 PM
the vizact.onevent function is pritty handy for this too

Penguin
05-12-2009, 02:42 PM
Hm, could you illustrate it on my sample code please? I had a look on the event classes but I don't know how to process my own event in class A respectively B and C.

The vizact.onevent documentation is unfortunately to scarce.

Gladsomebeast
05-12-2009, 03:10 PM
class A(object):
def __init__(self):
pass

def enableEvent(self, EventID):
vizact.onevent(EventID, lambda : (True,None),self.doit)