[SOLVED] How to get text input from user and store in a variable

So how can i store the written text in a varaiable

I’m assuming that the prompt will save the response that the person types into it.

Yes thank you i figured it out .

Hey how do I make it so it does a specific thing based on what you entered?

If you have the input stored in a variable check it’s value and execute an action in response

if( inputValue === 'Go to end level'){
   // e.g. fire an event
   this.app.fire('change:level:endLevel');
}
1 Like

I tried it and it does the same thing no matter what I enter

How is your set up? What do you use to get the text input of the user?

I mis worded it sorry it works as in it only fire when I input one of my commands but it also fires when I enter any of the other commands

HERE is my code

There is a typo in the loadScene of the ‘public’ part. Also you do the same in both parts. So where do you change the sceneName?

I have it as sconeName so that it doesn’t load the same scene

I think your current setup is incorrect. Try to replace your script with the script below. (Don’t forget to back-up your own script somewhere).

var Ui2 = pc.createScript('ui2');

// initialize code called once per entity
Ui2.prototype.initialize = function() {
   var input = prompt("Enter MATCH CODE ");
    
   if (input === 'TEST') {
       var self = this;
       setTimeout(function (){ 
           self.loadScene('TEST');
        }, 0.1);
    }
    
    if (input === 'PUBLIC') {
        setTimeout(function (){ 
            self.loadScene('BR ISLAND');
        }, 0.1);
    }
};

// update code called every frame
Ui2.prototype.loadScene = function (sceneName) {
    // Get a reference to the scene's root object
    var oldHierarchy = this.app.root.findByName ('Root');
    
    // Get the path to the scene
    var scene = this.app.scenes.find(sceneName);
    
    // Load the scenes entity hierarchy
    this.app.scenes.loadSceneHierarchy(scene.url, function (err, parent) {
        if (!err) {
            oldHierarchy.destroy();
        } else {
            console.error(err);
        }
    });
};

The Test Command Is Working Great But The Public Command Is Not

[ui2.js?id=46102747&branchId=722e806b-90af-4767-8829-42f1605b9f52:16]: Uncaught TypeError: Cannot read property ‘loadScene’ of undefined

TypeError: Cannot read property ‘loadScene’ of undefined
at https://launch.playcanvas.com/api/assets/files/Scripts/ui2.js?id=46102747&branchId=722e806b-90af-4767-8829-42f1605b9f52:16:18

Can I see your project please?

sure

https://playcanvas.com/project/767583/overview/explosive-battle-royale

I don’t see anything wrong. Can you try to load a different scene?

ok

Same error

I see what is the problem. Your variable self is not defined in the second setTimeout. An alternative for your ‘self solution’ is the way below.

    setTimeout(function(){
        this.loadScene('TEST');
    }.bind(this), 1);
1 Like

It Worked Thank You

1 Like

is there anyway I could do this in an actual text box instead of a prompt?

Hi @WilliamBoersma31! Maybe the topic below can help you.

https://forum.playcanvas.com/t/playcanvas-ui-components