Cannot reset camera


#1

The following:

function reset(){
var appC = this.app; var entityCamera= new pc.Entity();
this.entityCamera= appC.root.findByName(‘CameraMain’);
var posCam = new pc.Vec3(2.52, 0.266 , 1.598); var rotCam = new pc.Quat(4.81, 83.53,0);
this.entityCamera.setLocalPosition(this.posCam.x, this.posCam.y, this.posCam.z);
this.entityCamera.setLocalEulerAngles(this.rotCam);
}

gives me:
“Cannot read property ‘root’ of undefined”


#2

The error message is saying that appC is null/undefined


#3

We are actually coming back to my old problem -> making useful global variables. Right now I am shoving all my scripts in upon one hierarchy-entity only.
The ‘CameraMain’ is - for instance - the placeholder for the script above … does it hereby imply that the lines:
“var appC = this.app; var entityCamera= new pc.Entity();
this.entityCamera= appC.root.findByName(‘CameraMain’);” will fail due to the recurrance? (referrance back to own script-bearing entity)


#4

#5

Without seeing the context, that line implies a function scoped variable.


#6

There you made the code-section:

this.orbitCamera = this.entity.script.orbitCamera;
    
if (this.orbitCamera) {
    var self = this;
  1. -> does “if (this.orbitCamera) {” imply that you are asking the compiler if you have connection to the other script?

And 2) what does “var self = this;” help/mean?


#7
  1. It checks the if orbitCamera script object exists. It’s no different to if (this.entity.script.orbitCamera != undefined) given the code you have posted.

  2. It creates a new function scoped variable called self and points to the this object which in this case, the instance of the script. This pattern is commonly used with closures and removes the complexity of working out the scope of the this object. (See this link as an example : https://stackoverflow.com/questions/11780478/javascript-closures-this-keyword-and-variable-namespaces).


#8

Ok I remember/understand my thumbs-up for the other post (Other-script referal (Global variables)) as being a bit lucky (that why I deemed it solved back then)
Now:
var HotelShifter = pc.createScript(‘hotelShifter’);

// initialize code called once per entity
HotelShifter.prototype.initialize = function() {
var entityDetailHotel = new pc.Entity();
this.entityDetailHotel = this.app.root.findByName(‘Hotel23’);

var entityMinimHotel =  new pc.Entity();   
this.entityMinimHotel = this.app.root.findByName('Hotel_Minim');

this.ambientLight= this.entity.script.ambientLight;    

if (this.ambientLight) {
var self = this;
}

};

// update code called every frame
HotelShifter.prototype.update = function(dt) {
if(this.ambientLight.goCamera3w ===true){
console.log(‘OtherScript’);
this.entityDetailHotel.enabled = false;}
};

gives me >>
“TypeError: Cannot read property ‘goCamera3w’ of undefined”

  • where ambientLight is the script laying on another entity

#9

are the hyphens in the script-names important:
image

I cannot change ambientLight to ambient-Light in the code-editor :-/


#10

The filenames in the left pane? That’s just the physical file name. The script ‘name’ you should be using when trying to reference them is what is passed to the function of createScript.

eg

var HotelShifter = pc.createScript(‘hotelShifter’);

The script name is hotelShifter (case sensitive).


#11

The error message is saying that this.entity.script.ambientLight is undefined.

This bit of code literally does nothing.

if (this.ambientLight) {
    var self = this;
}

self is not used in the if statement nor the function that it is declared in.


#12


Those two scripts are on same entity


#13

The image shows an entity that doesn’t have an ambientLight script or hotelShifter script attached so I’m not quite sure what you are referring to? The entity in the image has mouseInput and orbitCamera.

When making replies like this, can you quote the text/post that you are answering to please? It helps when you have multiple questions going on in the thread.

If you can post a link to the project, it makes it much easier for people work out what’s the issue.


#14

The issue is that I am trying (with all my energy) to make scripts across different entities, read each others variables. On the previous single image above, the two scripts were not on different entities.

On this double printscreen, one can see that I have put the scripts on different entities. And then the project fails (no orbit function when launching).


#15

You can do this a few ways. PlayCanvas as a pretty decent event system and the methods shown in this Other-script referal (Global variables) thread.

Can you post a link to an example project and what you are trying to achieve in said project?


#16

Actually, I can see the tutorial project you are using.

Here’s one example using script attributes so the mouseInput script can reference the cameraEntity in the editor: https://playcanvas.com/editor/scene/681053

And another via this.app.root (which I don’t personally like using as it is tied to the name/tag in the editor.) https://playcanvas.com/editor/scene/681054


#17

Ok, thanks - I can tell that you moved the mouseInput script to Root :slight_smile:
Unfortunately I already tried that without luck …

Here is my project: https://playcanvas.com/editor/scene/680498
Press ‘Western coves’ and let it animate, after 6-8 sec the console.log (at F12-debug) should say true instead of false in accordance to.

var HotelShifter = pc.createScript(‘hotelShifter’);

// initialize code called once per entity
HotelShifter.prototype.initialize = function() {
var app = this.app;

var entityDetailHotel =  new pc.Entity();   
this.entityDetailHotel = this.app.root.findByName('Hotel_23');

var entityMinimHotel =  new pc.Entity();   
this.entityMinimHotel = this.app.root.findByName('Hotel_Minim');

var entityCamera =  new pc.Entity();   
this.entityCamera = this.app.root.findByName('CameraMain');    
   
this.amibientLight= this.entityCamera.script.amibientLight;     

};

// update code called every frame
HotelShifter.prototype.update = function(dt) {
console.log(this.amibientLight.goCamera3w);
if(this.amibientLight.goCamera3w ===true){
//console.log(‘OtherScript’);

this.entityDetailHotel.enabled = false;}

};

Press ‘Roof top’ (where I have placed my reset camera function). Which is my foremost goal.

cf

function reset(){

//var appC = this.app;  var entityCamera= new pc.Entity(); 

// this.entityCamera= appC.root.findByName(‘CameraMain’);
var self = this;
var posCam = new pc.Vec3(2.52, 0.266 , 1.598); var rotCam = new pc.Quat(4.81, 83.53,0);
//this.entityCamera.setLocalPosition(this.posCam.x, this.posCam.y, this.posCam.z);
//this.entityCamera.setLocalEulerAngles(this.rotCam);
self.setLocalPosition(posCam.x, posCam.y, posCam.z);
self.setLocalEulerAngles(rotCam);


#18

Same project using events: https://playcanvas.com/editor/scene/681055
And an app wide global: https://playcanvas.com/editor/scene/681062


#19

Not just that. I’ve made changes in both the script and the editor.

Looking at the project, there is nothing setting amibientLight.goCamera3w to true.

On a side note, you are creating 3 entities for no reason as you are not using them?

    var entityDetailHotel =  new pc.Entity();   
    this.entityDetailHotel = this.app.root.findByName('Hotel_23');
    
    var entityMinimHotel =  new pc.Entity();   
    this.entityMinimHotel = this.app.root.findByName('Hotel_Minim');
    
    var entityCamera =  new pc.Entity();   
    this.entityCamera = this.app.root.findByName('CameraMain');    

Given the context, you most likely want:

    this.entityDetailHotel = this.app.root.findByName('Hotel_23');
    this.entityMinimHotel = this.app.root.findByName('Hotel_Minim');
    this.entityCamera = this.app.root.findByName('CameraMain');    

#20

Ok I finally get it :slight_smile:

When having a (between scripts) commonly stated/defined camera entity:

.attributes.add(‘cameraEntity’, {
type: ‘entity’,
title: ‘Camera Entity’
});

in both scripts, one can dive down into this entity’s scripts (+ vars) [to whome it may concern in Unity-land: it is not enough to have the ‘GameObject’ in hierarchy alone]

PS: on the other hand you might want to reconsider the thing about ‘new pc.Entity();’-lines as my system failed in relation to finding the objects unless it was the ‘two line setups’