Alberto Torres avatar Alberto Torres committed bc2d941

Controller as decorator now works for instance methods too. Init reloader improved.

Comments (0)

Files changed (3)

 
-class Controller:
+class Controller(object):
     
     def __init__(self, function):
         self.f = function
         self.calling_sensor = None
         self.sensors = []
         self.actuators = []
+        self.instance = None
+        
+    def __get__(self, instance, klass):
+        # This allows the controller to decorate a method,
+        # which at the time of the decoration is still unbound.
+        # http://wiki.python.org/moin/PythonDecoratorLibrary#Class_method_decorator_using_instance
+        self.instance = instance if instance else klass
+        return self
         
     def __call__(self, *args):
-        ret = self.f(*self.sensors)
+        if self.instance is not None:
+            ret = self.f(self.instance, *self.sensors)
+        else:
+            ret = self.f(*self.sensors)
         for a in self.actuators:
             a(ret)
         
     def __list__(self):
         cdef gkGameObjectArrayIterator it = (<gkGameObjectArray*>self._p).iterator()
         
-        if self.list: return self.list
+        if self.list: return self.list  # TODO: what I was going to do with this?
         
         ret = []
         
 import gamekit, sys
-print sys.argv
+
 bname = sys.argv[0]
 ext = bname.split('.')[-1].lower()
 if ext in ('exe', 'bin', 'bin32', 'bin64'):
     bname = bname[:-len(ext)-1]
-bname += '.blend'
+    bname += '.blend'
+
 p = ''
 cfgfile = None
+# TODO: rewrite option parser
 for a in sys.argv[1:]:
     if p:
         if p=='-c':
         else:
             bname = a
 e = gamekit.Engine(bname,cfgfile)
+
 try:
+    print('Loading init...')
     import init
-    def r():
-         import time, traceback
-         gamekit.destroy_all_links()
-         loaded = False
-         tb_printed = False
-         while not loaded:
-             try:
-                 reload(init)
-                 loaded = True
-             except:
-                 if not tb_printed: traceback.print_exc()
-                 tb_printed = True
-                 time.sleep(1)
-         print('Reloaded successfully.')
-    gamekit.reload_init=r
 except ImportError:
     print("Error: Can't import init.py")
+    
+def r():
+    import time, traceback
+    gamekit.destroy_all_links()
+    loaded = False
+    tb_printed = ""
+    while not loaded:
+        try:
+            reload(init)
+            loaded = True
+        except:
+            tb = traceback.format_exc()
+            if tb!=tb_printed:
+                print(tb)
+                tb_printed=tb
+            time.sleep(1)
+    print('Reloaded successfully.')
+gamekit.reload_init=r
+
 e.run()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.