- Home /
Sending and Displaying a Series of Messages to the Player
So I need to be able to show a series of messages to the player ranging from warnings to in universe comms. These need to time out so they don't display permanently, and sort and display in the correct order. Ideally too it would be good if there is a cap to the number stored, so you don't end up with masses filling RAM.
This seems easy, but is proving to be beyond my (rather meager) programming skills. My first thought would be to make one long concatenated string, log the length of each string (or even just make lengths uniform) and then delete the characters as needed with StringBuilder() - but none of that seems to be present in unity. Then I tried making two arrays, one with strings, and one with the times the corresponding strings should stop getting displayed, but for some reason I could never get the for() loop to accurately find the oldest string. Then I tried copying what the Endless Sky team did - who did exactly this - and I can't even do that right. They seem to have done some sort of arcane magic with classes, but it turns out I don't really understand those/object orientated programming very well, and it doesn't help that it is all in C++.
Any ideas? I'm sure there is some simple solution that is totally passing me by.
Of course with a new day this ended up being far easier a problem magically. I've managed to get it working with: public class UI$$anonymous$$anager : $$anonymous$$onoBehaviour { public string[] $$anonymous$$essageArray = new string[5];
public void Add$$anonymous$$essage(string new$$anonymous$$essage)
{
$$anonymous$$essageArray[4] = $$anonymous$$essageArray[3];
$$anonymous$$essageArray[3] = $$anonymous$$essageArray[2];
$$anonymous$$essageArray[2] = $$anonymous$$essageArray[1];
$$anonymous$$essageArray[1] = $$anonymous$$essageArray[0];
$$anonymous$$essageArray[0] = new$$anonymous$$essage;
Invoke("ClearOldest$$anonymous$$essage", 5.0f);
}
private void ClearOldest$$anonymous$$essage()
{
if ($$anonymous$$essageArray[4] != null) $$anonymous$$essageArray[4] = null;
else if ($$anonymous$$essageArray[3] != null) $$anonymous$$essageArray[3] = null;
else if ($$anonymous$$essageArray[2] != null) $$anonymous$$essageArray[2] = null;
else if ($$anonymous$$essageArray[1] != null) $$anonymous$$essageArray[1] = null;
else $$anonymous$$essageArray[0] = null;
}
private void OnGUI()
{
//$$anonymous$$essage Display
GUI.Box(new Rect(0, 0, Screen.width, Screen.height), $$anonymous$$essageArray[0] + "\n" + $$anonymous$$essageArray[1] + "\n" + $$anonymous$$essageArray[2] + "\n" + $$anonymous$$essageArray[3] + "\n" + $$anonymous$$essageArray[4]);
}
}
I'm sure there is a less verbose way of doing this, but this works so I'm happy enough.
Your answer
