I want to add a script that is attached via attribute. My code works but is there a nicer way to check if it the entity has this script or to create it (do I need the name for that? I have the script already as asset in the variable this.runScript. When I use:
it doesn’t work anymore.
var DoAdditionalThings = pc.createScript('doAdditionalThings');
DoAdditionalThings.attributes.add('runScript', {
type: 'asset',
title: 'Script to run',
description: 'Script that will be executed'
});
// initialize code called once per entity
DoAdditionalThings.prototype.initialize = function() {
if(this.runScript.file){
var filenameWithSuffix = this.runScript.file.filename;
this.filename = filenameWithSuffix.split('.').slice(0, -1).join('.');
if(!this.entity.script.has(this.filename)){
this.entity.script.create(this.filename);
}
}
};
What I do is to trigger model animations and sometimes additional actions (like switch light on an off, change material, etc) from several triggers by using always the same script. Therefore I check if there is this script in place and simply add my individual script that should be executed. So I don’t need to care about any additional trigger events.