python - Posted event does not turn up with pygame.event.get() -
i putting component-entity game engine using pygame, , using custom pygame userevents messaging system (the way different subsystems within engine can communicate without real knowledge of each other).
the messaging system works attaching list of event types system. on each update cycle of engine class (which manages systems), filtered list of events system passed system, can act on events.
i have component factory creates component objects, attaches reference object event, , posts event. in problematic code testing, posting addinputcomponent
event (a custom pygame userevent) after creating inputcomponent
object.
in testing code can see create input system, install on engine proper event types, create input component, should fire off event.
i have verified code posting event occurring. have verified engine binding events input system properly.
you can see in update function inputsystem
, should handling event posted factory, never see event in list of events returned pygame.event.get()
.
engine
class pygameengine(object): def __init__(self, systems=none): self.systems = list() if systems none else systems def install_system(self, system, event_types=none): new_system = systementity(system, event_types) self.systems.append(new_system) def update(self, time, events): in self.systems: i.system.update(time, filter(lambda x: x.type in i.event_types, events))
system
class inputsystem(object): def __init__(self, components=none): # map of lists, map keys input devices, list items # components mapped device self.components = defaultdict(list) if components none else components def update(self, time, events=none): device, components in self.components.items(): comp in components: comp.last_state = copy.deepcopy(comp.state) print comp.state event in events: # system events if event.type == addinputcomponent: self.components[event.device].append(event.component) print "added new input component" elif event.type == removeinputcomponent: self.components[event.device].remove(event.component) elif event.type == updatebindings: pass
factory
def create_component(self, type, **props): component = none # inputcomponent if type == 'input': device = props['device'] entity_id = props['entity_id'] # todo: convert bindings bidict bindings = dict() if not 'bindings' in props else props['bindings'] component = inputs.inputcomponent(entity_id, bindings) new_event = event.event(addinputcomponent, device=device, component=component) event.post(new_event)
test code
inp = inputs.inputsystem() eng.install_system(inp, (addinputcomponent, removeinputcomponent, updatebindings, pygame.keydown, pygame.keyup, pygame.joybuttondown, pygame.joybuttonup, pygame.mousebuttondown, pygame.mousebuttonup)) t_entity = factory.create_entity() t_bindings = { 'up': pygame.k_up, 'down': pygame.k_down, 'left': pygame.k_left, 'right': pygame.k_right } t_inp_component = factory.create_component('input', device=-1, entity_id=t_entity.entity_id, bindings=t_bindings)
the game loop
while not(done): last_time = current_time current_time = pygame.time.get_ticks() time_since_last_update = current_time - last_time events = pygame.event.get() eng.update(time_since_last_update, events)
well, discovered problem is. documentation on pygame.event
:
all events have type identifier. event type in between values of noevent , numevents. user defined events can have value of userevent or higher. recommended make sure event id’s follow system.
http://www.pygame.org/docs/ref/event.html
noevent
"constant" of value 0, numevents
32, , userevent
24. posting event type value of 32 or greater causes pygame drop event, appears. means have 8 possible values custom events (numevents
- userevent
).
the event not seeing posted 39. defined this:
addinputcomponent = userevent + 15
i tried changing userevent + 1
, worked.
essentially, if want have more 8 types of custom events, have add property events post allows distinguish between event types.
also, limitation appears carried on sdl: http://www.libsdl.org/docs/html/sdluserevent.html
Comments
Post a Comment