RPG textboxes

Hi everybody,

AFAIK the only way to display text is via GUI, while I’m able to use it, the problem is that I want it to be textboxes.
Meaning that as soon you press a button, the textbox disappears and the next comes.

Unfortunately, OnGui is called every frame, which means I’d need to use a tons of if-cases for each message.

Now my question, is there another way, and how can one script it the most efficient?

Thanks ia

Hi, there are tons of plugins which do that for you in a really nice way (try NodeCanvas) but basically you could have a list (or tree depending if your story has options) and store the index of the current displayed item, all your button would have to do is increment the index.

string[] _speech = new string[4] {"Hi", "How are you ?", "Nice weather today", "Want some ice cream ?"};
int _speechIndex = 0;

void OnGUI()
{
     GUILayout.Label(this._speech[this._speechIndex]);
     if (GUILayout.Button("Next"))
          this._speechIndex++;
}
1 Like

I changed the code a bit, but for some reason the int always counts 2, instead of one.

string [] text = new string[5] {"Hello, fellow!", "how", "3", "four", "fünf"};
  int textnumber = 0;
  public GUISkin MyGUIskin;
   
  void OnGUI () {
  GUI.skin = MyGUIskin;
  GUILayout.Label (this.text[this.textnumber],"box");
  if (Input.GetButtonDown("Talk"))
  {
  this.textnumber++;
  }

I also don’t understand why I need to make a “new string”, or what the “this.” means.
Also, when I exceed the int limit, I get an out-of-int-array message in the console.

“this” means the current instance of this class, it is not required in C# as it is the default but I fancy the quick visibility of code it offers. Just remove it if you don’t like it.

new string[ ] means you instanciate an array of strings. If you make the variable public and define the values in the inspector unity will do that for you.

And yes you must make sure your int doesn’t go over the size of your array or you’ll run into an error. Try this, put 4 pens in front of you on your desk, then try to pick 5, you won’t be able to. Real life doesn’t throws errors but computer programs do when you attempt to get something that doesn’t exist !

It’s up to you to decide what you want to happen when the user has reached the end of the speech, should it close ? should it start over ? or maybe trigger something ?

if (this.textnumber + 1 == text.length)
{
    // Do stuff when speech is over
}
else
    this.textnumber++;

Edit:
Forgot to mention, do not use Input in the OnGUI() method, it does get called multiple times per frame so this is why your textnumber goes up by 2 instead of 1. You can either use Event.current from the OnGUI() or add an Update() to do the increment.

2 Likes

thanks works now, but two more question.
Why do I put the value 5 here?

new string[5] {"Hello, fellow!", "how", "3", "four", "fünf"};

Doesn’t an int counts from 0?, that’s why I need to make the “+1” in the

textnumber + 1 == textlength

unless I set my textlength bool to 4, but here it does count from zero.
So has the 5 nothing to do with counting, but just with the amount?

Second question:
I could probably figure it out myself, but since we’re at the topic how do I destroy the message again.
And, is it also possible to let the message appear letter-by-letter, and not the whole phrase in an instance?

Big thank so far, and furhter thanks in advance

You’re welcome but your really should read some C# basics tutorials, you would perform a lot better with only a bit of studying.

The 5 in the array initialization is the size, it is unrelated to the index used to acces values in the array. Index do start at 0 however you’re right, but try to declare an array of size 0 : new string[0] {};, How much do you store in that ? :smile:

I’ll let you read unity’s fine documentation or do a quick google search to find how to destoy the component or the gameObject :wink:

As for animating the length of strings appearing over time given a speed, I’ll let you meditate a little with this clues before pushing more hints :evil: