How do i change orbitCamera distance from another script


So what I’m going to do is in the multi-user case, one user zooms in on the camera and another user can see it. The socket I am using can pass the distance, but how can I change the orbitCamera distance once I receive it?
I used “this.app.root.findByName(‘Camera’).script.orbitCamera;”
But I can’t get distance in orbitCamera
It show undefind

1 Like

That should be fine as long as there is only one Entity called Camera in the scene. Can you share the project with this issue please?

sorry, It’s a private project
I can’t share the project
Now I have another problem
I don’t know how to use Object.defineProperty

Unfortunately, its difficult to help without a more complete example of the issue you are having.

Your original issue is mostly likely due to having multiple cameras or entities in the scene named the same (‘Camera’).

Do you know how to change orbitCamera distance from other scripts?

Yes, there are examples done with the input scripts in the orbit camera project.

https://playcanvas.com/editor/code/438243?tabs=6861120

However, the problem is with your specific project so unless you can share a public repro of the issue, its really difficult to help.

You can fork the project and remove all the private assets etc to share the issue publicly. We just need to see the relevant code and scene hierarchy

I have to apply for permission if I can fork

Can you show the full error message please?

image
image

image

If you search for ‘Camera’ in the hierarchy view in the Editor, how many entities does it find?

only one

In which case, a repro project of the issue needs to be shared. The code looks fine, it is likely to be a scene setup issue

two scripts are not on the same entity
main.js and orbitCamera

I prepare to remove all the private assets

I see you use pc.app.root instead of this.app.root. Are you sure this is correct?

Its not public API but it does work

1 Like

https://playcanvas.com/editor/scene/1419589
Removing private assets from the original project was too much, so I created a new project to do this.
Since socket communication is required, it is not clear whether you can connect.
But the effect is that the mouse wheel can deliver distance data.
I’m not sure how to modify the distance in orbitCamera

I just started using playcanvas, there are many things that are not clear

I see the issue, this is due to JS scoping and closures rather than PlayCanvas. (More on this here: You-Dont-Know-JS-1/README.md at master · jumaschion/You-Dont-Know-JS-1 · GitHub)


    socket.onmessage = function (msg) {
        console.log(msg.data);
        var result = JSON.parse(msg.data);
        var msg2 = JSON.parse(result.text);

        if (msg2.entity == "camera") {
            // console.log(msg2.distance);
            this.orbitCamera.distance = msg2.distance;
        } else if (msg2.entity == "camera2") {
            this.orbitCamera.pitch = msg2.pitch;
            this.orbitCamera.yaw = msg2.yaw;
        }

    };

You’ve created a function callback here but the scope of the this object is the function object, not the script instance object.

To fix this so that the this in the function is on the script instance so that you can access the properties, you bind the function to an object of the scope you want which is explained here: You-Dont-Know-JS-1/ch2.md at master · jumaschion/You-Dont-Know-JS-1 · GitHub

This would make your code:

    socket.onmessage = function (msg) {
        console.log(msg.data);
        var result = JSON.parse(msg.data);
        var msg2 = JSON.parse(result.text);

        if (msg2.entity == "camera") {
            // console.log(msg2.distance);
            this.orbitCamera.distance = msg2.distance;
        } else if (msg2.entity == "camera2") {
            this.orbitCamera.pitch = msg2.pitch;
            this.orbitCamera.yaw = msg2.yaw;
        }
    }.bind(this); // Bind the function to the scope of the script instance