Is there a way to know how many controls are stacked in a node?

Hi,

Is there a way to know how many controls are stacked in a node? I have a bunch of nodes with 0-1-2-3 controls attached to each of them, but it’s dynamic and I’m using an interface I made to manipulate them, so I kind of don’t know how many controls there are unless I make a variable in each of those classes and a getter function in the interface of those classes to obtain a handcoded value that I’d set myself. It’s pretty dirty.

I mean, the idea is something like this, but apparently, JME doesn’t like that:

[java]
// Destroy all prop controls
if(p.get_node().getControl(2) != null){
p.get_node().removeControl(p.get_node().getControl(2));
}
if(p.get_node().getControl(1) != null){
p.get_node().removeControl(p.get_node().getControl(1));
}
if(p.get_node().getControl(0) != null){
p.get_node().removeControl(p.get_node().getControl(0));
}
[/java]

EXCEPTION: java.lang.ArrayIndexOutOfBoundsException: 2 ----------------------------------------
com.jme3.util.SafeArrayList.get(SafeArrayList.java:258)
com.jme3.scene.Spatial.getControl(Spatial.java:667)
mygame.Main.reposition_spatial_and_or_control(Main.java:7195)

Any advice on the matter?


        for (int i = node.getNumControls() - 1; i >= 0; i--) {
            node.removeControl(node.getControl(i));
        }

i>=0

//untested however

1 Like

Yeah, every now and then it’s good to flip open the javadoc…

http://hub.jmonkeyengine.org/javadoc/com/jme3/scene/Spatial.html#getNumControls()

1 Like

Oopsies :wink:

Thanks to you both. getNumControls() is perfect. Sorry kthxbye. +1’ed both of you.

1 Like

:slight_smile:

@pspeed brilliant :smiley:

on topic, i use:


// Remove all controls from entity to execute possible cleanups on every control
while(node.getNumControls() > 0) {
    node.removeControl(node.getControl(0));
}

not confident about speed of this, @zzuegg 's version is probably faster

edit:
mygame.Main.reposition_spatial_and_or_control(Main.java:7195)
oou, split it into standalone classes before its too late! :slight_smile: 500 lines is optimal average i think :slight_smile:

From an api point of view i would probably prefer getControlsCount(), mainly because of alphabetical sorting in the javadocs.

@zzuegg said: From an api point of view i would probably prefer getControlsCount(), mainly because of alphabetical sorting in the javadocs.

And also when the SDK suggests while you type. That’s why at first I thought there was no such function, but again, maybe I stepped out of my chair a little too fast :wink: