0
\$\begingroup\$

I would like to create some sort of log to display previous strings for my game, but I still cannot figure out how to properly displaying them. I am using a button to run this method every time player clicks it.

public void AddLog()
{
    foreach (string logEvent in DisplayDialogueList)
    {
        logTextList.text = logText;
        logText += logEvent;
        logText += "\n\n";
    }
}

This method supposed to be displaying all the previous strings that has been displayed before inside DisplayDialogueList but I can't limit the displays only for the previous strings, and it will always display all the strings from the list.

I have tried the .Skip method to detect the current index of the list that are currently being displayed but it does not refresh the list, meaning if I loop to skip the first index from the list, it will always skips the first index even the current index to be displayed is second index and so on.

I also having trouble to skip the last index on the list since C# Unity does not support .SkipLast.

Any help would be appreciated.

EDIT: Sorry for the confusion, this is how I managed to distinguish which is the current string to be displayed, it runs on button click

public void Display()
{
    //Checks to see which element from the list has the same index number as the current index
    if((index == textIndex) && !(DisplayDialogueList.Count <= index))
    {
        //Displays the string from the list that has passed the argument above
        textList.text = DisplayDialogueList[textIndex];
        index +=1;
        textIndex = index;
    }   
}

Everytime the button is clicked, the index number will be increased, so it will matches with the string with the same index from the list and displays it.

UPDATE: Ok I managed to skip the current displayed index and last index (thanks to the answer below) but this happens apparently

Log is in the smaller fonts, while the current displayed string is the bigger ones below

The Hello, my name is Mira text is in the first index of the list. How do I make sure that text won't be displayed again after the back button is clicked, which run the AddLog()?

The updated code is below:

if(!(index > DisplayDialogueList.Count))
    {
        firstIndex = DisplayDialogueList.First();
        lastIndex = DisplayDialogueList.Last();
        foreach (string logEvent in DisplayDialogueList)
        {
            if(logEvent != lastIndex && !(firstIndex == currentIndex))
            {
                logTextList.text = logText;
                logText += logEvent;
                logText += "\n\n";
            }
        }
    }
\$\endgroup\$
  • 1
    \$\begingroup\$ For this you can use two list say previousList and currentList move the dialogue form current to previous. then display only previous. \$\endgroup\$ – Aryaman Gupta Sep 4 '18 at 9:29
  • \$\begingroup\$ How do we distinguish which strings in the list are "current" and which are "previous"? Is just the single last entry in the list "current"? (It looks that way from the rest of your question, but "current strings" is pluralised in the title, suggesting there might be more than one) \$\endgroup\$ – DMGregory Sep 4 '18 at 10:05
  • \$\begingroup\$ Updated my questions :) \$\endgroup\$ – Mira Sep 4 '18 at 10:25
  • \$\begingroup\$ @AryamanGupta I followed your method, but the previous list does not refresh itself and it will displays both updated previous list and before the updatedl previous list. How do I fix it? \$\endgroup\$ – Mira Sep 4 '18 at 10:37
1
\$\begingroup\$

You can get the last item in your list by something like this :

lastElement = DisplayDialogueList.get(size - 1) Where size is the list size.

Then in the for each loop you simply add a if statement :

If(logEvent != lastElement) { You log it.. }

Hope to understand your need

\$\endgroup\$
  • \$\begingroup\$ Unity does not recognise .get() method, or am I missing something here? \$\endgroup\$ – Mira Sep 4 '18 at 10:26
  • 1
    \$\begingroup\$ @Mira: often, code shared in an answer is pseudo-code, or a rough example of the kind of thing you want to do, not literal syntax to copy-and-paste. Here the corresponding C# syntax would be DisplayDialogueList[DisplayDialogueList.Count - 1] (I sometimes write this as an extension method so I can just call list.Last() for brevity ;) ) \$\endgroup\$ – DMGregory Sep 4 '18 at 10:47
0
\$\begingroup\$

Alright, after cramming for the whole night, I finally managed to solved this, I just add the displayed string to temporary list called Logs and just loop the Logs list and displays the added strings inside it before I clear them up, so it won't be any duplicates.

public void AddLog()
{
    if(!(index > DisplayDialogueList.Count))
    {
        //Iterate through the recorded logs
        for (int i = 0; i < Logs.Count; i++)
        {
            var log = Logs[i];
            var messageSameAsPrevious = i > 0 && log == Logs.Last();
            if(messageSameAsPrevious)
            {
                continue;
            }
            logTextList.text = logText;
            logText += log;
            logText += "\n\n";
        }
        //Clear the temp list
        Logs.Clear();
    }
}

Much simpler than my previous methods, and since for loop is much more efficient than foreach loop in terms of performance. Thanks to everyone here who gave me the ideas and insights, much appreciated :)

\$\endgroup\$
  • 1
    \$\begingroup\$ "since for loop is much more efficient than foreach loop in terms of performance…" While there can be performance differences between for & foreach, the gap isn't necessarily large, and is not significant when you're iterating over a handful of strings once per frame, so beware of premature optimization in areas like this. Sometimes a foreach is still worthwhile for simplicity/clarity/flexibility/ease of editing. \$\endgroup\$ – DMGregory Sep 5 '18 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.