Spatial.getController( class ) by Type

Would this sort of code be useful to others, I think a method like spatial.getController(MeshAnimationController); would be nice for initialization code, especially for noobs.  That way you don't have to know what order the Controllers were added. :slight_smile:



Here's the diff for com.jme.scene.Spatial.java:

@@ -369,7 +369,30 @@
         }
         return geometricalControllers.get(i);
     }
+   
+    /**
+     * Returns the first controller hopefully only
+     *    in this list of controllers with class c.
+     *
+     * @param c
+     *            The class to get a controller from.
+     * @return The controller with Class c.
+     * @see com.jme.scene.Controller
+     */
+   @SuppressWarnings("unchecked")
+   public <T extends Controller> T getController(Class<T> c) {
+        if (geometricalControllers == null) {
+            geometricalControllers = new ArrayList<Controller>(1);
+        }
+        for (Controller controller : geometricalControllers) {
+           if (controller.getClassTag() ==  c) {
+                return (T)controller;             
+           }
+      }
+        return null;
 
+    }
+
     /**
      * Returns the ArrayList that contains this spatial's Controllers.
      *


Maybe this should be moved to Contribution Depot for JME2.



Bump  :wink:

what about having two controllers (or more) of the same type? which one should be retrieved?


It will only return the first in the list.  I could rewrite it to return a ArrayList or Array, but I haven’t seen anyone add more than one controller of the same type in their code.



BTW I reposted this in the Contribution Depot here: http://www.jmonkeyengine.com/forum/index.php?topic=12970.0