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

Hi i’m Manoj.

I’m not getting how to use text input. Can you help me getting the text from user and show in a part of the screen. Please give me any tutorials or examples for text input to store data temporarily.

It’s not officially supported yet. Another forum user has kindly released their version of text input fields Released: Aphrodite Starter Kit UI Text Input

Another approach might be using some html input-text box and store the input in a variable and pass it to anywhere you want.

I know you are probably trying to get input through a game element, but here is a way you could prompt the user for input using a JS function.

var input = promt("Enter input: );

Sorry not clear. Can you give me any example please.

prompt will pop up a small window with a small input text where the user will write. evandarcy just made a couple of type errors.

var input = prompt("Enter input: ");

1 Like

Oops! My bad. Forgot to close that string.

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?