Helper methods to find specific spatials

Hello, first time posting here after some time lurking :smile: JME is awesome.

I was looking about the API for easy ways to find Spatials with certain userdata. I couldn’t find anything so I did a generic method that takes in a Comparable<?> value and looks in each spatial for that particular userdata (using a visitor)
Are there any built-in alternatives?

Thank you.

You can use a Comparator to acheive the same result as implementing Comparable. Except with Comparator you can customize the rules for comparison.

The scene graph traversal functionality should help you as well:

This is what I needed! Thank you!
I find myself using jme fine but everytime there is somethings more javaish I just know I don’t have the experience.

This was my first try:

public static ArrayList<Node> findAllNodes(final String udName, final Comparable<?> value) {
    final ArrayList<Node> result = new ArrayList<Node>();
    SceneGraphVisitor visitor = new SceneGraphVisitor() {
        public void visit(Spatial spat) {
    return result;

I’m sure someone will need something like this if I expand to more generic input + conditions I will post somewhere :stuck_out_tongue:

You don’t need the Comparable interface if all you are doing is .equals()… all objects in Java implement .equals().

Like that I would have to need to cast the values no? Maybe I was missing something.
This was the most general off the code solution I got to what I needed in the moment.
I couldn’t pass operators unless it’s some kind of enum I guess. I am now making something more generic.
Thank you.

You don’t need to cast to use .equals()… you don’t need to cast to use anything in the root class. Literally, you could remove the Comparable<?> from your code and it would still work with no other changes… but with that change it would support objects that aren’t Comparable.

Comparable is only for objects that can be sorted, ie: compared to one another for ordering.

Might I suggest doing some deeper Java tutorials sometime when you have a chance. They will cover this better than a forum thread here might.

Yeah sorry about it, will do.
Indeed Comparable<?> is not needed as you mention in this case but I do need the work for some cases. It’s looking good now.

Anyway what got me started was this simple example:
Sphere sphere = new Sphere(64, 64, dn.getUserData(“radius”));
needs to be?
Sphere sphere = new Sphere(64, 64, (Float)dn.getUserData(“radius”));
was wondering if I was doing something wrong.

Thank you.

That’s because Java cannot infer the type for the getUserData(). It doesn’t know at that level that Float is also float.

Where as, two separate lines will work fine:

Float radius = dn.getUserData("radius");  // Note: big 'F'
Sphere sphere = new Sphere(64, 64, radius);

…and then autoboxing can work. (Though also note there is a potential NPE if radius is null.)