Hi panda forums! I’ve been lurking for some time now and I was wondering if you could answer a question of mine? I am creating some basic movement for ralph and I currently calling the self.accept stuff in the init part of the class. I was wondering if it was ok to use a task to call a function to deal with the self.accept stuff instead of having it in the init part of the class.
Basically goes from this:
def __init__(self):
self.keyMap = {"w" : False,
"s" : False,
"a" : False,
"d" : False,
"mouse1" : False,
"mouse3" : False}
self.accept("w", self.setKey, ["w", True])
self.accept("s", self.setKey, ["s", True])
self.accept("a", self.setKey, ["a", True])
self.accept("d", self.setKey, ["d", True])
self.accept("w-up", self.setKey, ["w", False])
self.accept("s-up", self.setKey, ["s", False])
self.accept("a-up", self.setKey, ["a", False])
self.accept("d-up", self.setKey, ["d", False])
self.accept("mouse1", self.setKey, ["mouse1", True])
self.accept("mouse3", self.setKey, ["mouse3", True])
self.accept("mouse1-up", self.setKey, ["mouse1", True])
self.accept("mouse3-up", self.setKey, ["mouse3", True])
To this:
def __init__(self):
self.keyMap = {"w" : False,
"s" : False,
"a" : False,
"d" : False,
"mouse1" : False,
"mouse3" : False}
taskMgr.add(self.control, "Controls")
def control(self,task):
self.accept("w", self.setKey, ["w", True])
self.accept("s", self.setKey, ["s", True])
self.accept("a", self.setKey, ["a", True])
self.accept("d", self.setKey, ["d", True])
self.accept("w-up", self.setKey, ["w", False])
self.accept("s-up", self.setKey, ["s", False])
self.accept("a-up", self.setKey, ["a", False])
self.accept("d-up", self.setKey, ["d", False])
self.accept("mouse1", self.setKey, ["mouse1", True])
self.accept("mouse3", self.setKey, ["mouse3", True])
self.accept("mouse1-up", self.setKey, ["mouse1", True])
self.accept("mouse3-up", self.setKey, ["mouse3", True])
return task.cont
I know this still works as I have already tried it, but I was wondering if there was a better or “correct” way of doing this. You are probably wondering why I would do such a thing. The short answer is: I am attempting to load key strokes from a file and the only way I can accept a key is if I already know what that file says. For example: file says “w” is actually going to be “h” I would store “h” into self.keyw then apply it during control function via self.accept(self.keyw, self.setKey, [“w”, True]). Any advice or better ways of doing it would be nice. Thank you.