I’d like to build a text adventure game with graphical elements where players can select from a command list [help] list of commands in each scene. I’ve done a ton of research and tried out a couple of plugins but have yet to find what I need to get me started.
Specifically, I’m trying to start by:
Create a text box where players can input commands…
…that call functions in script that I have attached to an object in the scene.
As an example, I’ve got a scene with a 2D background image. I’d like the player to be able to type “North” into the text field, press enter, and have North correspond to a function that calls Application.LoadLevel for my scene to the north.
I’ve been able to find ways to create text fields for players to enter text, but not a way to have that text actually do anything. A few examples exist on how to save the text to player preferences but that doesn’t exactly help me right now either.
Full-out command parsing is complex but you can probably find examples for text games in other languages you can try to convert into something you can use here.
If you don’t want to parse everything, and it’s possible to list every single command in a big switch statement you could do something like that, too.
switch (input) {
case "north" : //do something
break;
case "south": //do something else
...
You’d at least want to track down a touppercase or tolowercase function for strings, so you don’t have to check for North or north or NORTH.
Thanks rrh. I tried to put the switch statement in Update but Unity didn’t like that. Now I’ve got it in Start, which I assume won’t really help me anyhow, because when does Unity know to check what the player has typed?
Here’s my code:
using UnityEngine;
using System.Collections;
public class TextAdventure : MonoBehaviour
{
public string input = Input.inputString;
void OnGUI ()
{
input = GUILayout.TextArea(input, GUILayout.Width(500), GUILayout.Height(300));
}
void start ()
{
switch (input)
{
case "north":
Application.LoadLevel("Test Room 2");
break;
default:
print ("Incorrect input");
break;
}
}
}
And here are the errors I’m receiving after running the game and typing into the text box:
get_inputString can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
ArgumentException: get_inputString can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
TextAdventure…ctor ()
Just check when the user validates his input by pressing enter for exemple. As for your error, is a fixed version of your code:
using UnityEngine;
using System.Collections;
public class TextAdventure : MonoBehaviour
{
public string input = ""; //An empty string is a perfect initial value here, Will fix the input string error as it says not to use that stuff here
private bool isValidated = false;
void OnGUI ()
{
if (Event.current.type == EventType.KeyDown Event.current.keyCode == Keycode.Return)
isValidated = true;
else
input = GUILayout.TextArea(input, GUILayout.Width(500), GUILayout.Height(300));
}
void Update()
{
if (isValidated) //Only Check once the user has validated his input
{
switch (input)
{
case "north":
Application.LoadLevel("Test Room 2");
break;
default:
print ("Incorrect input");
break;
}
isValidated = false;
input = ""; //Empty the input once it as been validated as the user would expect in most cases
}
}
}
ps: if the isValidated var bothers you, you may as well not use it by calling a method with the code of the Update() in it…