[Help] Text Input ala Classic Text Adventure

Hi All,

I have been working on the text adventure from the course, though I am pushing myself to do things a little differently.

I was hoping to write something to parse text input so that the player can do things like ‘open door’ or ‘pick up wallet’ (etc) by typing, rather than selecting the exact key press prompt. I’ve done so before when I used to write text adventures for school (in qbasic, and then C when I learned enough… and now I have shown my age!)

I am mostly looking at working out where I should be doing this, and how. Happy to do the research myself (as that is the best way to learn) but hoping someone can put me on the right track at least.

I have looked at two options so far: attaching an input field component to a text object, but this requires the player to select/focus on said object (and added to that, said box is formatted in Rich Text, which I am not yet sure how will go pushing that to a string in C#).

The second idea was to have the code in the update on a GameHandler (empty object w/script)

The psuedo-code (i.e. I am not looking at the code or documentation at the current point) I was thinking was along these lines:

public void update (){
if(Input.anykeydown) {
UpdateString(Input.currentkeydown); //that’s not a real function, btw, just not sure which one I am supposed to use
}
}

public void UpdateString(Keycode pressedKey) {
if(pressedKey == keycode.Enter) {
Parse(currentString) ;
}
else if {
currentString += (string)pressedKey ;
}
}

Like I said, this is currently psuedo code, and would not compile. I have just been trying to work out a few things:

  1. what function of Input.* would I need to use here.
  2. Is this the right way of going about it, as when I press, let’s say, the X key, there are going to be multiple cycles of the update process, so by the time I depress the key, the current string might end up as XXXXXXXXXXXX by the time I have let go of the key.

The ultimate plan would be to output the current string to a text object in unity, which I can format specifically. I am also aware that I would have to check to make sure that the keypress is a valid one, i.e. from a-z and A-Z and so on.

Cheers
Moose

Privacy & Terms