[SOLVED] Scripting 2.0 Engine side

Is there a special way of loading or telling the PC library that it is expecting Scripting 2.0 scripts or did I miss something?

world.js snippet:

//create boxes
function Box(x, y, z, sx, sy, sz) {
  var box = new pc.Entity();
  box.setPosition(x, y, z);
  box.setLocalScale(sx, sy, sz);
  box.addComponent('model', {
      type: "box"
    });
  box.addComponent('rigidbody', {
      type: "static"
    });
  box.addComponent('collision', {
      type: "box",
      halfExtents: [sx / 2, sy / 2, sz / 2]
    });
  box.model.model.meshInstances[0].material = createMaterial(Math.random(), Math.random(), Math.random());
  
  box.addComponent('script',{
    scripts:[{
      url:'assets/scripts/shape.js'
    }]
  });
  app.root.addChild(box);
  return box;
}

shape.js

var Shape = pc.createScript('shape');

// initialize code called once per entity
Shape.prototype.initialize = function() {
    
};

// update code called every frame
Shape.prototype.update = function(dt) {
    
};

// swap method called for script hot-reloading
// inherit your script state here
Shape.prototype.swap = function(old) {
    
};

error

playcanvas-stable.s2.min.js:700 script 'shape' has missing arguments in consructor
playcanvas-stable.s2.min.js:701 Uncaught TypeError: Cannot read property 'script' of undefinedget @ playcanvas-stable.s2.min.js:701d @ playcanvas-stable.s2.min.js:700(anonymous function) @ playcanvas-stable.s2.min.js:827(anonymous function) @ playcanvas-stable.s2.min.js:959(anonymous function) @ playcanvas-stable.s2.min.js:982e.onload.e.onreadystatechange @ playcanvas-stable.s2.min.js:983
playcanvas-stable.s2.min.js:693 Uncaught TypeError: Cannot read property 'length' of undefined
1 Like

The way you add script components to an entity has change slightly since the previous version. In your box script try this:

box.addComponent("script");
box.script.create("shape");

see here

I feel so silly now. It seems that when I downloaded the library through a playcanvas export, it wasn’t the new version of the script. I downloaded directly and it worked as expected. Thank you.