1

I'm trying to create a message queue for player messages and prompts. The goal is that any object in the game can just add an item to be displayed and it should handle the request. FYI - I'm pretty much a noob when it comes to coding/c#, so just trying to pick up the concepts and syntax at the moment, so my whole approach may be wrong to begin with!

I've decided to do it using a list.

List<GoalMessage> goalMessagesList = new List<GoalMessage>();

public static class GoalMessage : IComparable<GoalMessage>{
    public string textToShow;
    public int durationToShow;
    public bool specialFX;

    public GoalMessage (string myTextToShow, int myDurationToShow, bool mySpecialFX)
    {
        textToShow = myTextToShow;
        durationToShow = myDurationToShow;
        victoryFX = mySpecialFX;
    }
}

Then I create some test entries in the list:

void Start () {
    goalMessagesList.Add(new GoalMessage("List Item 1",2,false));
    goalMessagesList.Add(new GoalMessage("List Item 2 + FX",2,true));
    goalMessagesList.Add(new GoalMessage("List Item 3",2,false));
    goalMessagesList.Add(new GoalMessage("List Item 4 + FX",2,true));
    goalMessagesList.Add(new GoalMessage("List Item 5",2,false));
    goalMessagesList.Add(new GoalMessage("List Item 6 + FX",2,true));
}

and in the update I try to grab the first member of the list, throw it into my text display handler, so it's a FIFO style handler.

void Update () {

    if (textOn){
        break;
    }
    else if (goalMessagesList.Count>0){
    tempListItemToShow = goalMessagesList.FindIndex(0); //// ARGH - LOST HERE! /////
    }
    StartCoroutine(TextHandler (tempListItemToShow.textToShow, tempListItemToShow.durationToShow, tempListItemToShow.specialFX));

    goalMessagesList.RemoveAt(0);
}

The question is, how do I get out the first member of the list and then reference the variables stored against it so I can use them?

maraaaaaaaa
  • 7,749
  • 2
  • 22
  • 37

1 Answers1

0

The first member of your list would be goalMessagesList[0] or goalMessagesList.First() or goalMessagesList.FirstOrDefault().

To get the variables, just do:

string tts = goalMessagesList[0].textToShow;
int dts = goalMessagesList[0].durationToShow;
bool sfx = goalMessagesList[0].specialFX;
maraaaaaaaa
  • 7,749
  • 2
  • 22
  • 37