Transferring User Input ...

Okay, so I’m working on making a simple Mad Libs game, and right now I’ve been trying to test my ability to transfer the user’s input from simply one panel to another which would contain the created story.

However, after several hours of trying various things and Googling like crazy, nothing is working. It just keeps coming up blank. Right now I’ve been just trying to make it write “Hello (entered name)”

Once I get that, obviously I’ll be able to do actual stories.

How do you get the information entered into the InputField to move to my new panel which currently contains an empty Text box?

I apologize in advance if this has been posted many times in the past, I really didn’t want to resort to posting to ask for help but like I said… Several hours later, I’m too frustrated to continue Googling!

If anyone could help out, just explaining how I would do something very simple as making it write the “Hello Bob!” in the new story panel, that would be GREATLY appreciated!! :slight_smile:

Not sure here, but are you talking about writing to a new text UI object or to a (second) inputfield or what?

You could reference the second text object (whichever kind it is) and when they’re done typing, just copy it over.

Please explain exactly where you’re stuck and/or show the code you have (and maybe explain how you set it up in the scene).

You might want to try some basic UI exercises on the website (learning the editor/UI).

1 Like

Hey. I made a very simple example scene for you that shows how to use the input field component, store the text in a variable, and then output that variable to another text component. I’ve also commented all of the code to explain what it does. Hope it helps.

Input Field Example Project - http://s000.tinyupload.com/index.php?file_id=37630967201554169272

1 Like

I bet the problem is he doesn’t know how to substitute strings into the story text.

OK, here’s one approach. Define a little script that you will put on every input field, giving you a place to define which key (spot in the story) that field represents, and a convenient way to get the text that was entered:

using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(InputField))]
public class EntryField : MonoBehaviour {

    [Tooltip("Key to replace in the story with value from this field.")]
    public string fieldName;

    public string GetValue() {
        return GetComponent<InputField>().text;
    }
}

Attach this to your input field and then assign it a key, for example, maybe set fieldName to NAME. Now make a script that you attach to the story text, that creates the story by replacing fields in a template:

using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(Text))]
public class Story : MonoBehaviour {

    [Tooltip("Put story text here, with string like #KEY defining key values.")]
    [Multiline]
    public string storyTemplate;

    void Update() {
        // for easier testing/debugging, let's just update our story every frame:
        UpdateStory();
    }

    public void UpdateStory() {
        // Start with our template.
        string story = storyTemplate;
      
        // Loop over all entry fields...
        for each (var fld in FindObjectsOfType<EntryField>()) {
            // Get the key (name) of this field, and its current value
            string key = fld.fieldName;
            string value = fld.GetValue();

            // Substitute these into the story.
            story = story.Replace("#" + key, value);
        }
      
        // Update our text.
        GetComponent<Text>().text = story;
    }
}

(This code is off the top of my head and untested, but should be approximately correct.)

So make a big multi-line Text in your UI (don’t worry about hiding it for now), attach this script to it, and set its storyTemplate to something like “Hello #NAME!”.

Then just run. As you change the name field (the one whose EntryField.fieldName property is “NAME”), you should see the story text update accordingly.

When you’re ready, you can delete the Update method in the Story script, and instead call UpdateStory from the button that hides the input panel and shows the story panel. Easy peasy at that point.

1 Like

First of all… Thank you all for answering. Appreciate it more than I can type into this box!

JoeStrout - that worked PERFECTLY! The only thing I had to do was take the space out of “for each” and now I’ve actually got the input being transferred to the story. After so much frustration last night, this is greatly appreciated. Thank you!

1 Like

Nice – good stuff :slight_smile:

1 Like