Hi, I’m trying to change a scene when you click a button. Do you guys have any code that you can attach to a button entity that changes the scene when you click it? Thanks
Have you tried adapting some of these project samples to work with a button? https://developer.playcanvas.com/en/tutorials/?tags=scenes
Hey @yaustar
Yes i’ve seen those but i don’t know how they work. I’ll take another look at them and see if i can figure them out.
I took another look at them and they seem really confusing. They added a lot of stuff i don’t think i need. Could you give me some code that just changes scenes on a button click?
Hi @Charlotte_Darracq , “They added a lot of stuff i don’t think i need.”
What part of the code is it that you don’t think you need?
Check the following tutorial in addition on how to execute custom code when clicking a button:
https://developer.playcanvas.com/en/tutorials/ui-elements-buttons/
Hey @Leonidas
for buttons i am trying to use this code
this.entity.element.on(‘click’, function (event) {
//do whatever
}, this);
i don’t know if that works but i’m trying to attach that to a button entity and then put the change scene script inside of that. I just can’t figure out which part of the code in the tutorial I need and what part is for their specific project. Can you just send me the code i need to change the scenes inside my button entity code?
The basic scene changing project is a small amount of code https://developer.playcanvas.com/en/tutorials/changing-scenes/
There’s not much that is project specific in it
I’ve seen the tutorial @yaustar but i don’t know what all the of it does so i can’t modify it to use buttons. I just need to know what each part does and what parts i can modify to put in my function code.
Lets start with the initialise function, what is that doing?
i think it waits a second and then loads the scene. i think that’s the part i need to change but i’m not sure
Good, it’s loading the scene by calling a function that’s on the script. You can use that same function in your own code but instead of use timeout to call the function after a set period of time, you can do it on the click callback of a button press.
Depends on how you’ve hook up the event listener for the button.
Ok i figured it out. I’ll post the script i used in case anyone else wants to use it.
var ChangingScenes = pc.createScript(‘changingScenes’);
ChangingScenes.attributes.add(“scene1”, {type: “string”, default: “1003945”, title: “Scene ID to Load”});
ChangingScenes.attributes.add(“scene2”, {type: “string”, default: “1003943”, title: “Scene ID to Load”});
ChangingScenes.attributes.add(“scene3”, {type: “string”, default: “0”, title: “Scene ID to Load”});’’’
ChangingScenes.prototype.initialize = function(dt) {
this.app.root.findByName('Button').element.on('click', function (event) {
this.changeScenes(this.scene1);
}, this);
this.app.root.findByName('me').element.on('click', function (event) {
this.changeScenes(this.scene2);
}, this);
};
‘’'ChangingScenes.prototype.changeScenes = function(sceneId) {
// Get a reference to the current root object
var oldHierarchy = this.app.root.findByName (‘Root’);
// Load the new scene. The scene ID is found by loading the scene in the editor and
// taking the number from the URL
// e.g. If the URL when Scene 1 is loaded is: https://playcanvas.com/editor/scene/475211
// The ID is the number on the end (475211)
this.loadScene (sceneId, function () {
// Once the new scene has been loaded, destroy the old one
oldHierarchy.destroy ();
});
};
ChangingScenes.prototype.loadScene = function (id, callback) {
// Get the path to the scene
var url = id + “.json”;
// Load the scenes entity hierarchy
this.app.loadSceneHierarchy(url, function (err, parent) {
if (!err) {
callback(parent);
} else {
console.error (err);
}
});
};