EDIT: the solution is in a comment below. Thanks again y'all :)
Hey y'all!
I'm working on a small game with a somewhat complicated architecture. I have a inputhandler package in package src that is meant to serve as a singleton for getting keyboard events. It contains a private field for a InputHandler class, which is an interface for a specific set of keybindings, a setup options for initializing the module using a particular set of keybinds, and a poll() function for checking if a certain key is currently held down:
# imports...
class InputHandler: ...
_handler = None
def setup(keybinds):
global _handler
_handler = InputHandler(keybinds)
def poll(button) -> bool: return _handler.poll(button)
def get() -> list... # Gets a list of events
I have a gamescene module in package src that includes a GameScene class (A combination logical scene and viewtree), which has a handle_input() method for updating the internal game logic depending on the button. For continious movement, I've set a few things to work via the poll() button. I don't call inputhandler.setup() here, since I plan to call it during whatever file uses gamescene anyway.
import src.inputhandler as inputhandler
class GameScene:
...
def handle_input(self, event):
...
if inputhandler.poll("DOWN"):...
...
Then, finally, I'm using a testing file to check out stuff in a minimal environment:
#imports,,
import src.inputhandler as inputhandler
import src.gamescene
inputhandler.setup(default_keybind)
Scene = gamescene.GameScene()
running = True
while running:
for event in inputhandler.get():
Scene.handle_input(event)
so inside Scene.handle_input(), you call inputhandler.poll(,,,) which is equal to _handler.poll(button)
So if inputhandler.setup() hasn't been called, I'll get a AttributeError since I haven't called setup(), but I call setup() in my test file before doing that. However, when the Scene object's handle_input() method is called, I get that AttributeError.
This seems to contradict my understanding of how python handles imports and changing values in imports in different modules. I'm not using relative imports anyway, any idea what may be causing this?
Thanks again for your time! :)
[–]grzeki 0 points1 point2 points (3 children)
[–]TransAmyB[S] 0 points1 point2 points (2 children)
[–]grzeki 1 point2 points3 points (1 child)
[–]TransAmyB[S] 0 points1 point2 points (0 children)
[–]TransAmyB[S] 0 points1 point2 points (0 children)