Better way to execute an action when the user clicks off a mesh

It would be really nice if we had an action that fires when a user clicks anything other than the mesh the action manager is attached to. This would support, e.g. an unselect action. The only thing I have up with is this, which seems clunky, and doesn’t really leverage actions well. I am aware that I could do the whole thing with pointer observables; however, those fire on every pointer event, including move and we can’t leverage the power of actions. Is there a better way that I’m missing? If not, this would be a great enhancement to have to actions.

// Register an action to track when the pointer enters and exits the 
        // mesh
        this.actionManager.registerAction(new BABYLON.ExecuteCodeAction(
            { trigger: BABYLON.ActionManager.OnPointerOutTrigger }, 
            (e) => {
                console.log("Pointer out " + this.id);
                this._pointerIn = false;
            }
        ));
        this.actionManager.registerAction(new BABYLON.ExecuteCodeAction(
            { trigger: BABYLON.ActionManager.OnPointerOverTrigger }, 
            (e) => {
                console.log("Pointer over " + this.id);
                this._pointerIn = true;
            }
        ));

		// Register a pointer observable to detect clicks on or off the 
		//  mesh.  Can't use an action for this as it won't fire when we click off of the mesh
		scene.onPointerObservable.add((e) => {
            if (e.type === BABYLON.PointerEventTypes.POINTERTAP &&
                e.event.button === 0) {
                if (this._pointerIn) {
                    console.log("mesh was picked")
                } else {
                    console.log("something else was picked")
                }
            }
		});

Is this what you want? Each box is pickable. Click somewhere else to unpick. https://playground.babylonjs.com/#RC2IAH#1

1 Like

Thanks for the response. This is using pointer events. I was looking for a way to use actions.

Yeah there is no trigger for that currently. :frowning: but we’d welcome a PR for it :slight_smile:

The onAttachedToMeshObservable is actually invoked with an null-mesh for each detach event. However, also for any click on the background. It sounds easy to add a onDetachFromMeshObservable, yet I have no clue, how to even test it with the local Babylon copy after forking. The code should to be changed in here:

and somewhere in the docs.
A question is also whether the “onAttachToMeshObservable” should actually fire upon detaching.

Hi, Hope you are well.
(Once again), your post is quite interesting and might deserve to be looked at.
However, what do you expect from it? I mean you are replying to a 1y old topic marked with a solution.
My advise (if you want to pursue this discussion) would be to open a new topic and eventually a Feature requests so the Team and Community would give it another/fresh look at it. You could link this post as a reference. Else, it might just slip under the radar :face_with_hand_over_mouth: Of course, my opinion only and have a great day :sunglasses: