[SOLVED] A few Editor API questions

Hi, I love the PlayCanvas Editor API, but I can’t help but wonder am I missing something in my code , even when I do a even a few print tests with it. Currently using the documented Editor API from here:
https://github.com/playcanvas/editor-api/blob/master/docs/modules.md

  • 90% I get undefined or null prints with no other console errors.

Questions

  1. Should I be building locally with NPM?
    If so do I really have to or can I perhaps use a CDN link somehow possibly?.

  2. Should I be testing using the Violent Monkey extension only to allow partial embedding with my browser?
    Currently I am testing by merely pasting my code from an external text-editor.

  3. Do I have to call editor.call() for my code to work?
    I tried with and without and I’m not getting much luck with either still. ^^

  4. Currently I’m only using the documented Editor API like the API shown in the link above.
    However, I was also wondering is there still some private API?

  1. No. You don’t need the CDN either. The API is exposed on the Editor page.
  2. I use Volient Monkey. I generally do small testing directly in the browser devtools console. To make things easier, I connect Visual Code to Violent (instructions on Violent Monkey site). You can create your own browser extension too.
  3. No. Anything that is editor.call is ‘private’ API and subject to change. Some parts I’ve found are needed to get references to certain elements though.
  4. Yes there is. On the Editor page, open up devtools and find the file editor.js. you can see the whole of the Editor code there.

How are you using the API? Directly in the console? If so, not all operations return a value hence undefined.

For example, assigning a value to a variable doesn’t return a value for the operation.

As a quick example: Select an entity in the hierarchy and type the following on the console:

editor.selection.item

That should print out the object selected in the console

2 Likes

I assume you are using the example here on the docs? Editor API | Learn PlayCanvas

ok, thx.

So… I would need to create an extension even for testing? Or at least a function.
When I tried pasting in a function filled with code I got several line/indent syntax errors. Trying to find the correct way to code currently now. :eyes:

Getting/setting entities properties works fine for me. But only if they’re selected. If I try to get an existing asset for example I always get either said undefined/null or even a unrelated list.

Earlier sort of yes. But currently no. (still working on building the basics of code before I write my extension)

Example of what I’m trying to do (probably doing it wrong):

Tried re-coding my function to fit the Editor API examples… Getting null errors even though I do have a material called “Test.”

Is Assets only for objects?

The assets:findOne searches assets by function, not by a string name.

You can search an asset by its id with:

const asset = editor.assets.get(id);

Or, if you want to search by name, add a convenience function that first gets all the entities and then searches them by name.

const list = editor.assets.list(); // [ array of assets ]
2 Likes

No. You can test with the dev console (which is what I do) or with Violent Monkey. You don’t have to make your own extension but you can choose to.

Note that the assignment of a variable doesn’t return a value so it says undefined which is correct.

In the function you’ve shown, you aren’t using the public API as shown here and in my example above:

1 Like

I think my syntax is okay but still…

Did you run the same code multiple times in the console in the same tab session? If so, some someAsset variable is in global scope and as its const, it can’t be reassigned to another value.

Change it from const to let

And when you next assign it a new value, no need to define it again. ie

let someAsset = 4029483;
someAsset = 838474747;
1 Like

It works somewhat now.
How would I change a dynamic asset though? Say I’m searching for all scripts and I check the names for some of them and need to reassign/update my variable placeholder list.

Edit:

Hm… My looping code doesn’t seem to be updating either :thinking:
Both my attempts only get the last asset created.

Printing out the object only works in the devtools if just typed the variable directly, not as part of normal code.

In the case above, use console.log

console.log(i.get('name'));
1 Like

Both my methods work perfectly now! Thank you.
Some other questions:


Question #1

Which would be method is suggested?

let assets = editor.assets.list();

for (const i of assets){
    console.log(i.get("name"));
};

or

let assets = editor.assets.list();

for (let i=0; i < assets.length; i++){
    console.log(assets[i].get("name"));
};

Question #2

How would I return either of my methods?

You return a value in the function.

Doesn’t matter really. The first one is modern JS and is considered ‘better’ as there’s less risk of getting something wrong. Eg forgetting to use the i value. Using the wrong index if its a nested set of loops. If the code in the loop was longer, you wouldn’t have to temp assign the asset you need.

TLDR whatever is understandable to you and gives you less code to maintain.

1 Like