[SOLVED] I am trying to work with the loadscene() function

Here is my project:
https://playcanvas.com/editor/scene/1161778
here is the code:

var Trigger = pc.createScript('trigger');

// scene name is the scene ID
Trigger.attributes.add("sceneName", {type: "string", default: "", title: "Scene Name to Load"});

// initialize code called once per entity
Trigger.prototype.initialize = function() {
    this.entity.collision.on('triggerenter', this.onTriggerEnter, this);
};

Trigger.prototype.onTriggerEnter = function(entity) {
    entity.rigidbody.linearVelocity = pc.Vec3.ZERO;
    entity.rigidbody.angularVelocity = pc.Vec3.ZERO;
    // change the scene
    this.loadScene(this.sceneName);
}

When the bee collides with the finish line, I want to change the scene to the next level which can be any scene ID I choose. It seems to not be working, and I do not even get the attributes underneath the script. I have applied this script to the finish line or trigger. I could not find any documentation for this function, so if anyone knows what is wrong, I am happy to try answers!

Please refer to the tutorial examples here: Tutorials | Learn PlayCanvas

1 Like

In the examples they use it as a loop, I want to understand the syntax of function, because I am trying to use it in a different way.

There’s no loop in the examples though? Are you referring to the setTimeout call?

yes. not like an actual loop in coding, but like it changes the scene every second

I just want to understand the parameters that the function needs, and if I am using it wrong

Looking at your code,

var Trigger = pc.createScript('trigger');

// scene name is the scene ID
Trigger.attributes.add("sceneName", {type: "string", default: "", title: "Scene Name to Load"});

// initialize code called once per entity
Trigger.prototype.initialize = function() {
    this.entity.collision.on('triggerenter', this.onTriggerEnter, this);
};

Trigger.prototype.onTriggerEnter = function(entity) {
    entity.rigidbody.linearVelocity = pc.Vec3.ZERO;
    entity.rigidbody.angularVelocity = pc.Vec3.ZERO;
    // change the scene
    this.loadScene(this.sceneName); // loadScene function doesn't exist in the script
}

You don’t have the loadScene function defined in the script so it will throw an error.

You need to add the loadScene function from the example projects to your script.

ok, yeah I was wondering why it didn’t auto complete the function, I will give it a try!
I am still not seeing the attributes underneath the script. I am not sure why this isn’t working

I mean I am seeing the attribute, but I cannot edit it to change the scene ID.

Oh nevermind it was an issue with the name of the script! It seems to be working! Thanks!