- Home /
I want to create an in game log which will print actions that the player is making
This picture has an example of what i want to create:
http://www.dadsgamingaddiction.com/wp-content/uploads/2012/10/NEO1.jpg
At the bottom of the screen is a 5-10 line box which updates every time the player makes an action such as using items and then displays this action back to the player.
How would I go about starting this in unity, I have tried searching but I couldnt find any tutorials or guides to making this type of log.
Answer by Kevin002 · Aug 05, 2013 at 05:18 PM
Create a new script named PlayerLog
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayerLog : MonoBehaviour
{
// Private VARS
private List<string> Eventlog = new List<string>();
private string guiText = "";
// Public VARS
public int maxLines = 10;
void OnGUI()
{
GUI.Label(new Rect(0, Screen.height - (Screen.height / 3), Screen.width, Screen.height / 3), guiText, GUI.skin.textArea);
}
public void AddEvent(string eventString)
{
Eventlog.Add(eventString);
if (Eventlog.Count >= maxLines)
Eventlog.RemoveAt(0);
guiText = "";
foreach (string logEvent in Eventlog)
{
guiText += logEvent;
guiText += "\n";
}
}
}
then simply get the component and call the AddEvent Function to add the string you wish displayed. For example:
private PlayerLog eventLog;
void Start()
{
eventLog = GetComponent<PlayerLog>();
}
void Update ()
{
if (Input.GetKey(KeyCode.LeftArrow))
eventLog.AddEvent("Player Moves Left");
if (Input.GetKey(KeyCode.RightArrow))
eventLog.AddEvent("Player Moves Right");
}
the result is this
Agreed. I've updated my answer to add Scrolling effect and Swapped TextArea for Label with the Skin of a text area to keep visual.
This solution does not appear to do anything when the number of lines exceeds the size of the box
I appreciate the advice. I didn't take much time to properly set up the code I wrote thinking it was just an example and not something that would be integrated line for line. I didn't think about the fact that most people simply copy/paste the code they see on these answers.
I do agree however that it should be done properly. I've reformatted the code to only rebuild the string when adding an event. I've also removed direct access to the list and added the AddEvent function ins$$anonymous$$d.
I was translating robertbu's example to C# in the interim which uses a similar approach but also might serve as a basic example of using Queue ins$$anonymous$$d of List:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class playerLog : $$anonymous$$onoBehaviour {
public int maxLines = 8;
private Queue<string> queue = new Queue<string>();
private string $$anonymous$$ytext = "";
public void NewActivity(string activity) {
if (queue.Count >= maxLines)
queue.Dequeue();
queue.Enqueue(activity);
$$anonymous$$ytext = "";
foreach (string st in queue)
$$anonymous$$ytext = $$anonymous$$ytext + st + "\n";
}
void OnGUI() {
GUI.Label(new Rect(5, // x, left offset
(Screen.height - 150), // y, bottom offset
300f, // width
150f), $$anonymous$$ytext,GUI.skin.textArea); // height, text, Skin features
}
}
Thank you both $$anonymous$$evin and Robertbu, from this post and your examples, I've now got a working game log which has been on my ToDo list for some time, I hope AParker does too.
I want to clear my log, but every time it writes a new line it has all of the old lines. Anyone has an idea how to fix it?
Answer by robertbu · Aug 05, 2013 at 04:47 PM
My depth with GUI is not great, so there might be a better way. But here is a bit of sample code that displays some lines of text. Attach script to a game object and hit play:
#pragma strict
import System.Collections.Generic;
var maxLines = 5;
private var queue = new Queue.<String>();
private var testStrings = ["Eating crackers", "Stomping foot", "Trying lock", "Drinking potion", "Picking up dagger", "Hitting the wall", "Turning off lights"];
private var text = "";
function Start() {
InvokeRepeating("Test", 0, 2);
}
function Test() {
NewActivity(testStrings[Random.Range(0,testStrings.Length)]);
}
function NewActivity(activity : String) {
if (queue.Count >= 5)
queue.Dequeue();
queue.Enqueue(activity);
text = "";
for (var st : String in queue)
text = text + st + "\n";
}
function OnGUI() {
GUI.Label(Rect(0,0,500,300), text);
}
Your answer

Follow this Question
Related Questions
Turning RPC logging off 1 Answer
What's the difference between print() and Debug.Log() 1 Answer
How do you get console messages in order? 0 Answers