I presume all the ‘.enabled’-methods are deprecated.
But how to use .render “show()” and “hide()”, in the new hierarchy?
I have used them earlier, but cannot find the specific projects (code snippets) in a good/fast way :-/
The api-sources does not have any real examples.
All in all: As I dont get the same autocomplete feedback as earlier, I am a bit challenged.
ok, for now I will try the “findComponents”-method … will get back to the enabled-issue at another time (but in case you haven’t used .enabled for a while, would you try it in a 5 minute example? → I have namely had trouble with it on three occasions the last months)
Press Space to toggle the the render components enabled property that will make the vehicle appear/disappear
Relevant code:
// update code called every frame
PressSpaceToToggle.prototype.update = function(dt) {
if (this.app.keyboard.wasPressed(pc.KEY_SPACE)) {
var renderComponents = this.entity.findComponents('render');
for (var i = 0; i < renderComponents.length; ++i) {
renderComponents[i].enabled = !renderComponents[i].enabled;
}
}
};
I’ve also added the code to disable/enable an Entity when you press 1
Relevant code:
var Press1ToToggle = pc.createScript('press1ToToggle');
Press1ToToggle.attributes.add('targetEntity', { type: 'entity' });
// update code called every frame
Press1ToToggle.prototype.update = function(dt) {
if (this.app.keyboard.wasPressed(pc.KEY_1)) {
this.targetEntity.enabled = !this.targetEntity.enabled;
}
};
It’s a script on a separate entity because rather than on the entity that gets toggled because when the Entity is disabled, the update in the script is not called so the code to detect the keypress is not ran to enable it again.
var ToggleLayer = pc.createScript('toggleLayer');
// initialize code called once per entity
ToggleLayer.prototype.initialize = function() {
this.layerBx = this.app.root.findByName("Box");
};
// update code called every frame
ToggleLayer.prototype.update = function(dt) {
if (this.app.keyboard.wasPressed(pc.KEY_SPACE)) {
this.layerBx.enabled = !this.layerBx.enabled;
}
};
So will dive into look for the the error myself (should be able to compare and find it … or somehow copy project and chissel down the lines, to see where it goes wrong)