[quote=“Monster,post:7,topic:634423"”]
see agoose77’s explanation.
There is no speed difference.
if you read the explicit form it would be:
module.DICT[k] vs self.DICT[k]
I also meant to turn it to a class variable rather than a class member. So you only get one instance for all objects (less memory). As you have just one instance of each class it does not really matter.
You can use “bge.logic.keyboard.events” inside your class. It tells the reader where it comes from and that there is a dependency. You have that dependency already … why not using it?
A shorter name (see agoose77’s post) might help writing, but is not really required.
In the new code you can remove the sub-classes completely. The instances are configured via configuration .
keyboard = Input(bge.logic.keyboard, "KEY")
mouse = Input(bge.logic.mouse, "MOUSE")
This is as you could mix the configuration and classes:
mouse = Keyboard(bge.logic.mouse, "KEY")
Alternative you can “hard-code” the parameters in the sub-classes as in your first post.
With the use case the module makes more sense.
I still think “actives” is a bit … strange looking. I suggest to name it “pressed” or “pressedKeys”
alternatives:
- pressed(key)
- justPressed(key)
- released(key)
- justReleased(key)
keys() with an integer does not provide more handling than the existing bge method. It even reduces the amount of information to two states .. which can be expressed as boolean (True/False).
If you do not want to loose this information, how about a KeyStatus object?
...
def getKeyStatuses(keys):
return <list of KeyStatus>
...
class KeyStatus():
__init__(self, defaultAsciiKey): ...
pressed(self): ...
justPressed(self): ...
released(self): ...
justReleased(self): ...
than you can write things like that:
w,a,s,d = myinput.keyboard.getKeyStatuses("w","a","s","d")
if w.justPressed():
...
if s.pressed():
...
surely named with your names and conventions :).
[/quote]
There is no speed difference.
if you read the explicit form it would be:
module.DICT[k] vs self.DICT[k]
You can use “bge.logic.keyboard.events” inside your class. It tells the reader where it comes from and that there is a dependency. You have that dependency already … why not using it?
probably i falled fully in the “premature optimization” trap
but the difference exist as -> (timelocal:timeglobal) = (4 : 3)
X
is ever more fast than
X.X
that is ever more fast than
X.X.X.X.X.X
import time
DICT = {str(n): n for n in range(50)}
R50000 = list(range(50000))
def test():
class C:
def __init__(self):
self.DICT = DICT
def test(self):
k = "1"
t1 = time.clock()
for _ in R50000:
v = DICT[k]
t2 = time.clock()
print("use global: ", t2-t1)
t1 = time.clock()
for _ in R50000:
v = self.DICT[k]
t2 = time.clock()
print("use local: ", t2-t1)
c = C()
c.test()
anyway … “optmize later” is ever a good idea.. 
If you do not want to loose this information, how about a KeyStatus object?
w,a,s,d = myinput.keyboard.getKeyStatuses("w","a","s","d")
if w.justPressed():
...
if s.pressed():
...
except the name of method ( i not like much is the name -> “getKeyStatuses”),
seem a very good solution, since is absolutely readable
and get a supershort code(supposing one want just a list of bolaeans) is anyway possible using a pretty basic list comp .
w,s,a,d = [i.pressed() for i in myinput.keyboard.XX(“w”,“s”,“a”,“d”)]
the names of method of KeyStatus are instead absolutely clear.(not omissions)
in my opinion this will be a “good buildin”!
class KeyBoardKeyStatus:
def _get_status(self): #main method
return bge.logic.keyboard.events[self._number]
def released(self):
return self._get_status() is 0
def just_pressed(self):
return self._get_status() is 1
def pressed(self):
return self._get_status() is 2
def just_released(self):
return self._get_status() is 3
PS: But the real implementation has some part redundant hard to remove (90 lines :evilgrin:)