[SOLVED] Simple script asset not initializing

https://playcanvas.com/project/1132340/overview/cybers-cafe-fp-infinite-gltf

home.js is not initializing…

var Home = pc.createScript('home');


// initialize code called once per entity
Home.prototype.initialize = function() {

  console.log("update");

  //entityPlacement();

}


Home.prototype.update = function(dt) {
    console.log("update");
};

...

I have tried getting rid of everything else but the console.log().
Other scripts like Network.js have no problem initializing.
I tried rearranging the script loading order and even tried to call initialize manually but to no avail.

Once this is working I need to also debug raytracing. home.js is supposed to be a debugger for an error i see in my development environment (self-hosted):

playcanvas-stable.min.js:6 Uncaught TypeError: s.get_m_closestHitFraction is not a function
    at i.raycastFirst (playcanvas-stable.min.js:6:1190954)
    at Raycast.doRaycast (raycast.js:59:45)
    at Raycast.doSelect (raycast.js:46:14)
    at Raycast.mouseDown (raycast.js:30:12)
    at t.fire (playcanvas-stable.min.js:6:19995)
    at i._handleDown (playcanvas-stable.min.js:6:234456)
i.raycastFirst @ playcanvas-stable.min.js:6
Raycast.doRaycast @ raycast.js:59
Raycast.doSelect @ raycast.js:46
Raycast.mouseDown @ raycast.js:30
t.fire @ playcanvas-stable.min.js:6
i._handleDown @ playcanvas-stable.min.js:6

Hi @Jordan!

It looks like home.js is not attached to an entity. That’s probably why the script is currently not working.

Can you try to attach the script to an entity?

Sure thing!

It’s loading now. Some new problem came up - the raycast.js isnt being ‘enabled’. But I put a fix just for this project (read on if you are interested)
Try it again you should now see the error message .

Raycast.prototype.mouseDown = function (e) {
    if (this.inputEnabled) {
      this.doSelect(e);
    }
};
...
Raycast.prototype.enableInput = function () {
  this.inputEnabled = true;
};

whenever mouseDown is called (after home script has loaded and has executed raycastEntity.enableInput() …),

clicking and triggering mouseDown does not call doSelect() because this.inputEnabled is For Some Reason turned back to false.

It is only doing this in Playcanvas… So I just set inputEnabled = true in Raycast.prototype.initialize(). It’s triggering the mouseDown events now.

And again it is the the same error message I mentioned: get_m_closestHitFraction is not a function

Thanks @Albertos

This happened when I upgraded the PlayCanvas engine from v1.26.0 to v1.65.5.

There are some changes in raycasting in one of the past updates, so I will suggest to check the API and update the way of raycasting if necessary.

I will take a look at your project when I have some time.

i updated it according to https://developer.playcanvas.com/en/user-manual/physics/ray-casting/

I can’t figure out how to get the camera I guess. In the project that is linked there
https://playcanvas.com/project/410547/overview/entity-picking-using-physics

it breaks with the same error. i tried to add the camera (apparently this method is deprecated too)


Raycast.attributes.add('camera', {
    type: 'entity'
});

in initialize():

    // Find the first entity in the hierarchy with the name 'Camera'
    this.cameraEntity = this.camera;
debug.js:85 Powered by PlayCanvas 1.65.5 026dc4259
debug.js:107 WARNING: This version of ammo.js can potentially fail to report contacts. Please update it to the latest version.
warn @ debug.js:107
messenger.js:78 messenger connected
system.js:535 Uncaught TypeError: rayCallback.get_m_closestHitFraction is not a function
    at RigidBodyComponentSystem.raycastFirst (system.js:535:33)
    at Raycast.doRaycast (raycast.js?id=4608603&branchId=1624a7ae-8e76-465d-8a1c-6f8e6de4ac27:47:45)
    at Raycast.mouseDown (raycast.js?id=4608603&branchId=1624a7ae-8e76-465d-8a1c-6f8e6de4ac27:29:10)
    at Mouse.fire (event-handler.js:240:26)
    at Mouse._handleDown (mouse.js:273:14)
system.js:535 Uncaught TypeError: rayCallback.get_m_closestHitFraction is not a function
    at RigidBodyComponentSystem.raycastFirst (system.js:535:33)
    at Raycast.doRaycast (raycast.js?id=4608603&branchId=1624a7ae-8e76-465d-8a1c-6f8e6de4ac27:47:45)
    at Raycast.mouseDown (raycast.js?id=4608603&branchId=1624a7ae-8e76-465d-8a1c-6f8e6de4ac27:29:10)
    at Mouse.fire (event-handler.js:240:26)
    at Mouse._handleDown (mouse.js:273:14)

Be aware the camera for raycasting is probably not the entity itself, but the component of the entity.

// camera entity
this.cameraEntity = this.camera;

// camera component
this.cameraEntity.camera;

It also look like that you need to update the Ammo version in your project.

one fix was using cameraEntity.camera.screenToWorld() instead of cameraEntity.screenToWorld()

thanks to this post https://forum.playcanvas.com/t/screentoworld-offset/29498

The problem remains that

this.app.systems.rigidbody.raycastFirst

is breaking in both projects (mine and the raycasting demo)

Did you update the Ammo version in your project?

Idk, according to https://developer.playcanvas.com/en/user-manual/physics/calling-ammo/ PC has it built in as a version from 2016.

I “import” ammo now it works! thank you

strange that, when i import it into my node host it doesnt error, but it doesnt find any object.

Sorry, not sure what you mean with this.

I download the project as a zip and host it with nodejs. It’s not live right now so I can’t really show you the demo for about a week.

I have no experience with that myself.

Please open a new topic for that problem when you are ready.

It’s working now. Thanks for your help. I upgraded the engine for GLTF support & I’ll open an issue if I have any problem getting that going.