[SOLVED] Script isn't added to ScriptRegistry

background
I have a local copy of playcanvas 0.192.0 which is being served via a nodejs web server, together with
the minimal example of a spinning cube found in the sources. I made a few small changes.
I am trying to to play around with the first-person-movement script, which I found here https://playcanvas.com/editor/scene/329712

problem
I have tried to simply add the script to the head of my page, but the script isn’t added to the ScriptRegistry
and I am entirely sure whether it is supposed to; app.scripts.list() returns an empty array.
Beside including the movement script, I have a construction like this

  player.addComponent("script", {
        scripts: [{
            url: 'first_person_movement.js'
        }]
    })

And I am wondering, what’s the purpose of url ? I see no outgoing request, so I assume playcanvas
doesn’t request this for me?

Now I am at the point where I have something like this

var controller = pc.createScript("first_person_movement");
player.addComponent("script");
player.script.create("first_person_movement");

I now get something back form app.scripts.list(), but it is not the actual function of
first-person-movement.js I am looking for.

I am trying to move a player entity, which has all the required components to be moved by
the first-person-movement script, my main problem is that I don’t receive any errors or see any requests.

questions

  • Should url request the script for me, or do we have to make our own xhr function?
  • Should a script included to the page add itself to the ScriptRegistry?
  • What is the correct way to apply a script to an entity? because it seemed really straightforward here:
    https://playcanvas.github.io/#camera/first_person/index.html

Hi @ozette.

So confusion you get is between legacy scripts and script 2.0. We’ve updated some time ago scripts system to new one, making old scripts boilerplate incompatible with new system.

Here is a user manual for new scripting system: http://developer.playcanvas.com/en/user-manual/scripting/
It pretty much explains everything.

In your case the link you’ve posted uses legacy system (https://playcanvas.github.io/camera/first_person/first_person_camera.js), you need to change it slightly:

So your script definition will look like that:

var FirstPersonCamera = pc.createScript('first_person_camera');

FirstPersonCamera.prototype.initialize = function() {
    // initialize code here
};

FirstPersonCamera.prototype.update = function(dt) {
    // update code here
};

FirstPersonCamera.prototype.onMouseMove = function(event) {
    // mouse move event handler
};

FirstPersonCamera.prototype.onMouseDown = function(event) {
    // mouse down event handler
};

It can be defined before you add script to component, either within same js file or loaded as separate js file.
Old way where you say in script what file to load is no more, as now you can have many scripts defined per single file.

So adding to component is the way you wrote indeed.

Let me know how you getting.

1 Like

Awesome thanks, I managed to get a script attached to an entity in the new way thanks to the link
you shared.