Hi Rob,

Thank you very much for your response. I have attached a simplified version 
of the code and, yes, here I clearly see that more than one event arrives 
in an iteration of the loop.

How can I register the observers only once each iteration? 

I have not found any example that process the events like this, could you 
tell me where I could find some?

Thank you,
jl

El dimecres, 25 febrer de 2015 19:12:24 UTC+1, Rob va escriure:
>
> Hi,
>
> I just took a short look. As I see you call my_dispatch_events() in your 
> event loop. Each time it registers a new set of event observers that all 
> use the same event object to store the results.
>
> The problem here is that it is possible that more than 1 event arrives in 
> 1 iteration of the loop. In that case the event object is overwritten with 
> the last event. Also it is not necessary to keep registering new observers. 
> This will probably in the end cause some kind of memory and resource leak.
>
> It is better to have the observers registered only once and then have them 
> store each event in a Queue (or list). Then in your loop process the 
> contents of the Queue before going to the next iteration.
>
> Rob
>
> Op woensdag 25 februari 2015 17:33:25 UTC+1 schreef Jose Luis Da:
>>
>> Hi all, 
>>
>> I am using pyglet to develop a series of visual stimuli. I have some 
>> problems with my code, could you help me understand what I am doing wrong?
>>
>> I have attached a part of the code I am using, the main file is called 
>> "150121_Plaid_v19", and "rlabs_libutils" is a library of some functions, 
>> one of which I will explain next.
>> In lines 190 - 258 I have the stimulus loop (or game loop) and for each 
>> iteration I need to retrieve the subject's input and save it in an array. 
>> In order to do that, I use the function 'my_dispatch_events', which I call 
>> in line 204 with the pyglet window and an instance of a class called 
>> LastEvent() that I defined.
>>
>> With this method, I lose some of the data. Could you help me understand 
>> what would be a good method of retrieving input events and saving them for 
>> each iteration of the game loop, while using as few global variables as 
>> possible (I would like to keep the pyglet window instance as a local 
>> variable)?.
>>
>> Thank you very much for your attention,
>> jl
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"pyglet-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pyglet-users.
For more options, visit https://groups.google.com/d/optout.
import pyglet 

class mypygletwindow:

	def __init__(self):
		self.winHandle = pyglet.window.Window()
		self.winHandle.on_mouse_press 	= self._onPygletMousePress
		self.winHandle.on_mouse_release = self._onPygletMouseRelease
		self.winHandle.on_key_press		= self._onPygletKeyPress
		self.winHandle.on_key_release	= self._onPygletKeyRelease
		self.winHandle.getevent			= self.getevent

		self.lastevent = {}

	def _onPygletMousePress(self, x,y,button,modifiers):
		print 'press', button, (x,y)
		self.lastevent = {'type':'MOUSEDOWN', 'button': button, 'pos': (x,y)}

	def _onPygletMouseRelease(self,x,y, button, modifiers):
		print 'release', button, (x,y)
		self.lastevent = {'type':'MOUSEUP', 'button': button, 'pos': (x,y)}

	def _onPygletKeyPress(self, symbol, modifiers):
		print 'press', symbol, modifiers
		self.lastevent = {'type':'KEYDOWN', 'symbol': symbol}
	
	def _onPygletKeyRelease(self, symbol, modifiers):
		print 'release', symbol, modifiers
		self.lastevent = {'type':'KEYUP', 'symbol': symbol}

	def getevent(self):
		out = self.lastevent
		self.lastevent = {}
		return out

if __name__ == '__main__':

	w = mypygletwindow().winHandle

	while not w.has_exit:
		dt = pyglet.clock.tick()
		w.dispatch_events()
		w.clear()
		
		# get events
		e = w.getevent()
		if e != {}:
			print e
		w.flip()

Reply via email to