Reputation: 47
What I'm trying to do is define controls for a certain thing in another module which defines everything else for that thing, then run the controls from the main loop via a function.
The problem with trying to call it with a function, from what I've tried, is that the events that the function sees only match the events when the function was defined at the program start instead of the current events; meaning, no events.
Here's an example of what I mean:
main file
from pygameinitmodule import *
import othermodule
#event_list = pygame.event.get() is run in pygameinitmodule to define it for all modules
running = True
while running:
#updating the event_list
event_list = pygame.event.get()
if certaingamestate:
othermodule.controlFunction()
othermodule
from pygameinitmodule import *
def controlFunction():
global event_list
for event in event_list:
# controls here
And if I am then to run the code, the controls don't work. But if I put the exact same code from the function into the main loop it works fine. Is there another way I'm supposed to do something like this, or am I doing it mostly right while overlooking one critical part?
Edit: to the person that marked this as a duplicate, it's not. The question you marked this as a duplicate of is focusing on a different thing.
Upvotes: 0
Views: 380
Reputation: 6156
You can pass the event list as an argument to the function:
other_module.py
def controlFunction(event_list):
for event in event_list:
# do stuff
And in the main loop use as such:
main_file.py
while True:
event_list = pygame.event.get()
controlFunction(event_list)
Or just import pygame
in the other file and get the events there (this approach, however, is not suggested since it removes events from the queue and they can't be retrieved from other places):
other_module.py
import pygame
def controlFunction():
for event in pygame.event.get():
# do stuff
Upvotes: 2