Object interaction/action systems

Hello there!
So i’ve just finished looking through the collision/picking objects tutorial and I have a question. Before I start implementing I want to check i’m not doing this in the most silly way possible.

I basically want to begin acting on objects, turning lights on and off, opening and closing doors or for inanimate objects just showing the user a description of them.

For the light, I can simply add/remove lights from the geometry. But for other objects, am I going to have to do something like:

[java]
String geometryName = closest.getGeometry().getName();
if(geometryName.contains(“door”)) {
// rotate door.
} else if(geometryName.contains(“knife”)) {
displayText(“This is dangerous…”);
}
[/java]

Or is there some way I can do object.doAction for the varying types of objects? The only way I can think of doing this is a bit messy, adding objects to a hashtable, looking them up by name to retrieve them and calling doAction() or doAction(int id) with doAction being different for each type of object.

Likewise by doing this i’d be able to do object.getActions() in order to display a list of possible actions of the object to the user.

Is there a better/standard way to do this? I was thinking ‘Controls’ which would check the ‘action id’ and act accordingly? Although if in all the control update methods it’s something like

[java]if(actionID == 1) {
// turn on light
}[/java]

I’d imagine all the lights with the controls would turn on.

Thankyou :slight_smile:

You could add a control to the object to handle the actions the object can perform.

1 Like
@pspeed said: You could add a control to the object to handle the actions the object can perform.

Hey, sorry, had updated the post regarding controls.

Since controls call ‘update’ on simpleUpdate - wouldn’t that mean that all objects implementing my custom control would be called? Thus, if I had a ‘LampController’ and set that, all the lights would turn on?

EDIT: Actually just realised one can do getControl() and update a boolean or something to set that control to turn that particular light on. Was thinking controls being universal for some stupid reason…

Great, thankyou. JMonkey has so many things so perfectly built in :).

Oop,s double post sorry.

edit: think I spotted my stupid mistakes hehe.

Your trouble is certainly that the collision is detected on the geometry level, whereas your control is added at the node level.

Try something like geo.getParent().getControl(DogControl.class);

@yang71 said: Your trouble is certainly that the collision is detected on the geometry level, whereas your control is added at the node level.

Try something like geo.getParent().getControl(DogControl.class);

Yep, just realised as I was on my break haha :). Thanks yang.

Code for anyone else with this mistake:

[java]
CollisionResult closest = results.getClosestCollision();
Geometry geo = closest.getGeometry();
Spatial s = geo.getParent();
InteractionControl ic = s.getControl(InteractionControl.class);

      if(ic != null) {    
        td.setToolTip(ic.getDescription());
      } else {
          td.setToolTip("Looking at " + geo.getName() + " s name " + s.getName());
      }

[/java]