Hey guys I am very new to Unity and I want to make an application just like a basic C# Console application.
My Project has a Panel (black background), a TextBox (where all the output is written) and a InputField (where the user writes the input).
I want to use one script to handle everything if possible.
When my TextBox says “Press 1 to Continue or 2 to exit the game” I then want to wait for the user input to handle it correctly.
I can’t use the Update() function because this “1 or 2” won’t be the only Input I want to handle. Like I said it’s gonna be a text based adventure game.
So the question is: How can I wait for the user Input in my Start() function and stop the code from processing until an Input was made?
It’s completely fine if you forward me to a tutorial by the way, I just couldn’t find what I was looking for. :<
Text based adventure game, very cool
I think that you could handle it in your update function, if you have certain times when you need a specific input, place those checks in there.
bool waitCondition false;
// situation occurs that requires '1' to continue or '2' to exit
waitCondition = true;
// inside update
if(waitCondition){
// if input is 1 or 2 , act accordingly
return;
}
// if that ever gets to a point that interrupts your data flow or is insufficient, you could adjust it a bit?
Why not use Unity Actions? Pass what you want to happen as a parameter then you’re able to fire it once you’ve received user input.
Button myYesButton;
Button myNoButton;
public void WaitForUser(string question, UnityAction yesEvent, UnityAction noEvent){
//Add the actions passed to a UI object, for example a button.
//Show some UI dialog, or anything that prompts user feedback.
//Remove all listeners first
myYesButton.onClick.RemoveAllListeners();
myNoButton.onClick.RemoveAllListeners();
//Add the new events
myYesButton.onClick.AddListener(yesEvent);
myNoButton.onClick.AddListener(noEvent);
}
public void PromptUser(){
WaitForUser("Are You Ready?", new UnityAction ( () => {
//Your Yes Action
}), new UnityAction( () => {
//Your No Action
}));
}
Wow, that was a fast answer. Thank you very much!!
You mean like that?
void Update()
{
bool waitCondition = true;
while (waitCondition)
{
if (Input.GetKeyPressed("return"))
{
if (InputField.text == "1")
{
//Do That
waitCondition = false;
}
else if (InputField.text == "2")
{
//Do That
waitCondition = false;
}
}
}
}
My problem with the update() function is that it gets executed every frame, so when I type in “1” again and press enter it will jump right into that -if (InputField.text == “1”)-Part again.
I think(!) what I need is the C# equivalent for Console.Readline();
But Unity doesn’t have that included
I’m so confused.
Daamnnn that’s some code I need to understand before I answer.
You are using lambda expressions right?
Thanks for your code example but you handled my problem with buttons if I’m seeing right.
Buttons in a text based adventure games are a no go
But I’m really glad you guys are helping me out!!
Edit: Now I understand your code a little better… If that’s going to work in my case that would be so awesome. I’ll try it right after work! :v
You could always use a callback and do whatever you need to do with the user input?
public void getUserInput (string question, Action<string> response)
{
//You can use anything to confirm the user input, in this case, a confirm button and an input field
confirmButton.onClick.AddListener ( () => {
response(inputField.text);
}
}
public void PromptUser ()
{
getUserInput("Some Question", (response) =>
{
//response is now available in the callback and can be passed to another member.
SomeOtherMethod(response);
}
}
You could even set up a co-routine to wait for a keystroke or look for it in your update function. I appreciate this is quite rough but it may get you where you need to be.
Yeah, the unity Event is a good idea, but I didn’t want to offer that because it was text based.
Okay, sorry for glossing over this detail. You want to go when the input is 1 not just 1 clicked.
and you shouldn’t do “while()” in update in this case, either, since that’s inferred by the update function loop cycle anyways. (You’d force the code to always stay there, and not continue the main game loop, in other words).
You want to setup you input solution from your inputfield (which is actually sort of like ‘readline’)
Apologizes for not fully thinking that through lol.
oh geez, i’m half asleep it seems. You already had the return key there for the condition. Wow.
Your issue is that you’re setting the waitCondition to true inside update!!!
Move that outside! Only set it true when you need it on
Indeed – there is a callback for submitting in the inputfield, but creating your own with ‘enterKey’ is perfectly okay, too.
The unity built-in thing sometimes has to be messed around with/modified (Unless you use TMP) to get it to behave more nicely.
Which reminds me, OP you might want to try TMP I mean not to make this work… just in general.
And I think my post just before this one helped clear up [2 posts’ back] confusion lol
Wow I love you guys.
I have to try it when I come home, my fingers are starting to itch because I want to do it now. xD
Thanks for the patience and I’ll post an update no later than tomorrow if I got it to work or not.
I wanna die immediately.
I can’t make it work, I suck at Unity so much.
Ok. are you guys sure that I can get where I want to if I orientate myself from your code snippets?
Again, this is a simple C# example how I want my code to work:
public main()
{
Console.WriteLine("Hello, do you want to go left or right?");
if (Console.ReadLine() == "left")
{
Console.WriteLine("You went left. You see a monster. Will you flee or lay on the ground playing dead?");
if (Console.ReadLine() == "Flee")
{
Console.WriteLine("As you were trying to flee you died immediately. End.");
}
else if (Console.ReadLine() == "Play dead")
{
Console.WriteLine("As you were trying to play dead you died immediately. End.");
}
}
else if (Console.ReadLine() == "right")
{
Console.WriteLine("As you were trying to go right you died immediately. End.");
}
}
Can i beg someone to write me a simple script where my C# example is covered?
Soorrraaaaaayyyyyyyyyyyyy… :<<<<<<<<<
Well, say you send a message to the player… somewhere you have to have “What you’ll do based on their input”.
So, essentially you’ll be “blocking” (which is what Console.ReadLine()) is doing When the player provides his or her input, you will have a small list of values to check against for the action to take, right?
// Use this for initialization
void Start()
{
textPanel.text = "Welcome!";
textPanel.text += "\n" + "To start the game, press 1, to exit the game press 2.";
input.ActivateInputField();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("return") && !string.IsNullOrEmpty(input.text))
{
if (input.text == "1")
{
textPanel.text += "\n" + input.text;
textPanel.text += "\n" + "Game started!";
}
else if (input.text == "2")
{
Application.Quit();
}
}
}
This example works for the first question the player is asked just fine.
But when I now tell him “Press 2 to kill the monster” and he presses 2, the application will exit because my I put my code in the Update function which gets repeated every frame. I mean I could create hundreds of global booleans which would Skip the parts I don’t need in my Update function but that would be the worst programming I’ve ever done. xD
And I can’t make a list of answers like you suggested because would have to be unique which isn’t the best thing. I’m soooo confused!!
Okay, obviously I see what you mean so far, but for each "Step " in the game, you’ll need possible outcomes.
Then you can “switch” , based on the direction you’re going. That could be a literal switch / case statement or something similar. For instance, (and it’s tough, because this is general), but…
say you have a monster is attacking: User gets 5 options. You set a variable to say “Monster fight” , then accept input based on that scenario. So, Maybe “Pick up item” (random example) that is tied to key “3” in some other situation, can’t be activated because of your variable about the monster fight. I hope that makes sense/offers some direction.