actionManager and boolean flags

Hi.

I have a question to optimize my code.
I use the actionManager to click on a mesh and toggle the camera-view.
The Playground works like I want:
https://playground.babylonjs.com/#DGKGVJ

But if I have 10 or 100 meshes Iam not able to reduce my code. With my “skills” I would write the complete actionManager-block for every mesh and just adjust the isClicked-var (like sphere_isClicked) and the actionManager-var (like actionManager_sphere).

Any ideas? I am grateful for every tip!

So, as I understand, you have 1 camera which will change its position and target when clicked on specific mesh?

Thanx for your help!

I have a main camera (overview).
And for every mesh I have a detail camera.

So if I click on a mesh I switch to this camera and if I click again I switch back to the main camera. Thats working fine in the PG.

For this situation I use boolean variables for each mesh.
lets say: sphere_isClicked, cube_isClicked, mesh3_isClicked… and so on
i need this variables global for other decisions.

Iam not able to change these boolean variables in a function, so I always have to write nearly the same code over and over again. You can see it in the Playground example.

a function should be something like:
setMeshAction (mesh, camera, clickState);
But I dont know how to change the boolean variables inside of the function globally?
I would like to change the global boolean variables inside of the function.

Thanx!!!

OK. When I use a Object, than it work like I want.
I didnt know that I cant change global values inside functions.

1 Like

Hey @Rizn
May I suggest an alternative?

You could use

scene.onPointerPick = (evt, pickInfo) => {
   // do stuff here
}

Created simple playground.
https://playground.babylonjs.com/#7C20S1

FYI. If you want to make sure only certain meshes are clickable, you can metadata property on the mesh once it is created and then inside onPointerPick you can check for that.

Eg.

var box = BABYLON.MeshBuilder.CreateBox("box_"+i, {size: 1}, scene);
box.metadata.canClick = true;

end then

scene.onPointerPick = (evt, pickInfo) => {
   if (pickInfo.pickedMesh.metadata.canClick) {
    // do something magical with global property (OH NOOOOOO!)
  }
}

Enjoy!

I would even say that if you use Object you may use separate .js or .json file to import needed values.

Also, from my point of view, to have separate camera for every detailed mesh view is overkill. It is enough just to have 1 camera and switch it’s position and target.

1 Like

Totally agreed! :smiley:

Secondary scene with own camera could do too (depending what @Rizn is trying to achieve really). Although that would require reloading the asset. Haven’t found a way to share assets between scenes yet effectively yet. :thinking:

Hi Mark.
Thank you for your alternative. I will have a look!
It seems I will also have trouble with a lot of boolean variables (if cameraWasClicked == camera2 ETC)

This is a version with the object (just because of the need to change the obj.isClicked variable inside of the function) https://playground.babylonjs.com/#DGKGVJ#1
thats why i use the object.

(the new version with the object) https://playground.babylonjs.com/#DGKGVJ#1
isnt this a good solution to use the object just because of the need to change the obj.isClicked variable inside of the function?

(I used more cameras because i had trouble to switch between freeCamera and arcRotate, but i will think about it! Thank you for this overkill-hint!!!)

1 Like

Nice one @Rizn. It is looking good.

Your multi camera playground have given me interesting alternative to my problem.
In my game was trying to go with separate scenes and cams but single scene approach with using multiple asset containers and single camera as described in this thread.
After reading this thread, however, I realised switching between game view (arc rotate cam) and tech view (fixed in place ortho cam) cameras is even better for my use case.

Ok, above might seem a bit off-topic but…
I want to say that there isn’t single good / bad solution @Rizn . Just do what feels more comfortable and/or will be easier to maintain.

@Rizn and @labris - Thank you both