How to use functions step by step

Viewed 100

Sorry for the strange title, I just don't know how to name this question.

So I have such a function say().

void say(string printedText) {
    gameText.text = printedText;
}

And I need to use it several times. Something like this:

say("test text 1");
say("test text 2");
say("test text 3");
...

I need to change text by clicking Space button. Of course I need to use something like this:

if(Input.GetKeyDown(KeyCode.Space)) {
   ...
}

But I can't understand how to show text step by step. So for example if I click Space button once I should see "test text 1". Next click should show me "test text 2" etc.

How can I realise it? Thanks in advance.

3 Answers

Depending on your needs you could store different texts in a List<string> or maybe even Queue<string> and do

List example

// Add your texts in the editor or by calling texts.Add(someNewString)
public List<string> texts = new List<string>();

private int index = 0;

if(Input.GetKeyDown(KeyCode.Space)) 
{
    // have a safety check if the counter is still within 
    // valid index values
    if(texts.Count > index) say(texts[index]);
    // increase index by 1
    index++;
}

Array example

Basically the same as the List<string> but you can't add or remove elements "on the fly" (at least not that simple)

public string[] texts;

private int index = 0;

if(Input.GetKeyDown(KeyCode.Space)) 
{
    // have a safety check if the counter is still within 
    // valid index values
    if(texts.Length > index) say(texts[index]);
    // increase index by 1
    index++;
}

Queue example

public Queue<string> texts = new Queue<string>();

for adding a new text to the end of the queue do

texts.Enqueue(someNewString);

and then

if(Input.GetKeyDown(KeyCode.Space)) 
{
    // retrieves the first entry in the queue and at the same time
    // removes it from the queue
    if(texts.Count > 0) say(texts.Dequeue());
}

Simple counter

If it is really just about having a different int value then yes simply use a field

private int index;

if(Input.GetKeyDown(KeyCode.Space)) 
{
    // uses string interpolation to replace {0} by the value of index
    say($"test text {0}", index);
    // increase index by one
    index++;
}

define a class field like this:

int count = 0;

and now everytime space is hit:

if(Input.GetKeyDown(KeyCode.Space)) {
    say("test text " + count);
    count = count + 1;
}

This Code:

if(Input.GetKeyDown(KeyCode.Space)) {
   ...
}

only works for Unity, In Visual Studio you'll have to create an Event to whatever object you want to do this, for example, if you want to call a void each time you press spacebar youll have to do this, it is easy: (Image below)

In the propieties window press the bolt icon and after double click on the event you want to create (samples): TextChanged, LocationChnaged, MouseMove, etc...

I will use KeyDown on the TextBox object

image

Now in your code this void should be generated

image

Inside this void i writed the code and this is how it looks:

(put int n = 1 before voids)

private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Space)
            {
                //int n = 1; must be defined
                textBox1.Text = "test text " + n; 
                n++;
            }
        }

Now each time you'll press or stay pressed spacebar the textbox will be filled with "test text " and the value will be 1 more each time.

Related