Scripting the PlayCanvas Editor

Hi @SunnyChow, I have some code to showcase that. I am not on desk currently, I’ll post it later today.

Here you go, the following code will add a script component to the selected, in the hierarchy, entity, attach a script type (orbitCamera) and at the same time set the value of an attribute:

var selectedEntity = editor.call('entities:selectedFirst');

editor.call('entities:addComponent', [selectedEntity], 'script');

var script = 'orbitCamera';
selectedEntity.set(`components.script.scripts.${script}`, {
    enabled: true,
    attributes: {
        distanceMax: 90
    }
});
selectedEntity.insert('components.script.order', script);
1 Like

Hi Leonidas,
I’m trying to set the entity attribute through editor code in a script attached to the current selected item.
But I’m getting observer errors, how can i set the entity into my entity attribute field/variable?
For context: in this example i wanted the script to link to itself just for testing, but I’m not sure what type of value it is expecting.

async function setLinkedEntity() {
      const type = editor.call("selector:type");

      if (type !== "entity") {
        return false;
      }

      const items = editor.call("selector:items");

      if (!items || items.length === 0) {
        return false;
      }
      //console.log('items:', editor.call('selector:items')[0].json());

      items.forEach(async (item, index) => {
         editor.call("selector:set", "entity", [item]);
        item.set('components.script.scripts.linkEntity',{
          enabled: true,
          attributes: {
            linked: item
          }
        });    
      });

      console.log("--- Finished execution 7 ---");
    }

Hi @fcsa,

I gave it a try on a project, to reference an entity to an attribute you only need to pass the entity guid.

Example code for a script type named myScript and an attribute named linkEntity:

item.set('components.script.scripts.myScript.attributes.linkEntity', item.get('resource_id'));

This will reference itself.

1 Like

Thank you!

Is there a way to use “drawLine” in the editor viewport?
I have installed violentmonkey’s extension and I find it quite usefull, I was wondering if there was a way to draw helper lines, like we do using this.app.drawLine but in the editor.

You can use that same method, the editor runs a regular pc.Application instance. The only difference is it doesn’t update the scene automatically (autoRender is set to false).

So to render a line per frame you will have to write your own update method. For quick setup a setInterval can do that:

window.setInterval( () => {
    pc.app.drawLine(new pc.Vec3(0,0,0), new pc.Vec3(0, 10, 0), pc.Color.WHITE);
}, 0); 

1 Like

hehe i kinda figured this out testing, but still thank you for your prompt response!
Here is a much complex one, I haven’t being able to figure out.

If anybody is interested here is how i was able to add rotation to my selected item

var lookMat = new pc.Mat4();
var rotation = new pc.Quat();
var pos1 = new pc.Vec3(items[0].get('position')[0], items[0].get('position')[1], items[0].get('position')[2]);
var pos2 = new pc.Vec3(items[1].get('position')[0], items[1].get('position')[1], items[1].get('position')[2]);    
var direction = new pc.Vec3().sub2(pos1, pos2);
lookMat.setLookAt(pc.Vec3.ZERO, direction, pc.Vec3.UP);
rotation.setFromMat4(lookMat);
var r = rotation.clone().getEulerAngles();
items[0].set('rotation',[r.x, r.y, r.z]); 

I’d say that since this is a regular PlayCanvas coding question post a new thread about it, so we don’t pollute this old but super useful topic.

1 Like

Hi again,
Is there a way I can grab the entity in the viewport with a resource_id?
for example if my selected item has a script referencing an entity how can i get the reference to the entity using its resource_id to change its rotation in the editor viewport? item.get(‘components.script.scripts.myScript.attributes.linkEntity.attributes.target’);

Sure, it’s as simple as this:

const entity = editor.entities.get('7dee54h4-4eb0-4700-be5f-1b3d481504g0');
2 Likes

THANK YOU SO MUCH!

1 Like