Finding a component using a string

play canvas already implements:
entity.script.has( strScriptName );
entity.script.create( strScriptName );
entity.script.destroy( strScriptName );

however there isnt a:
arrScripts = entity.script.get( strScriptName );

is there a shorthand way to achieve this way without having to iterate the script list… Thanx

Hi.

Let’s have a look at has function:

function(name) {
    var scriptType = name;
    if (typeof scriptType === "string") {
      scriptType = this.system.app.scripts.get(scriptType);
    }
    return !!this._scriptsIndex[scriptType.__name];
  }

Seems like, all scripts owned by system and entities only implement it.
Since that, you can use app.scripts.get(name) to get your script.

1 Like

That will be:

var script = entity.script.strScriptName;

or

var script = entity.script["strScriptName"];
1 Like

-mikefinch: Not exactly what I was looking for however a cool trick to get all the instances of a particular script used throughout ALL ENTITIES …

-Max: Careful!!! the strScriptName is NOT the name of the script I want, but points to “scriptThatIwant”…
var script = entity.script[strScriptName];
I was not aware that you can use the brackets on entity.script …

You just wrote a solution yourself.
Examples I’ve provided are just various ways that JavaScript allows to traverse object keys.