I have a script that takes the camera as an attribute and I want to add it to an entity and populate the attributes using Javascript. I’ve figured out how to add the script (confusingly using entity.script.create) and can add attributes that are simple number or text but I can’t work out how to specify an entity as an attribute? I’ve found some info in the docs but it doesn’t mention this issue (createScript | PlayCanvas API Reference).
The code I’m using (which doesn’t work) is;
var cameraEntity = this.app.root.findByName('Camera');
// create the script
rotateEntity.script.create('rotate', function(context){
attributes: {
cameraEntity: cameraEntity
}
});
Press space to spawn a new box (ideally findByName shouldn’t be used as is O(n) look up, I would have a reference to it in the script and pass it to the create function).
In your case, the second param is supposed to be a JS Object, not a function.
ie
var cameraEntity = this.app.root.findByName('Camera');
// create the script
rotateEntity.script.create('rotate', {
attributes: {
cameraEntity: cameraEntity
}
});
findByName works by iterating through the hierarchy from graphNode/Entity it is called from until it finds (or not finds) another graphNode/Entity with the name provide.