- Home /
I am making a click based game. I need to print out a log of the actions(clicks) made by the person and store that log to be used later. How can i do that?
Hello, I am working on a game in which the user will be clicking on different buttons to move(play the game). I need to print out the log of the buttons pressed by the person and store them in a dataframe in unity. For example, if there is a button called "go", coloured green, when the user presses it, the log should generate that > "go" button pressed. I really need your help. Also, how can i access the dataframe after the game ends. I need it to be stored so i can view it after the game. Thank you so much.
Answer by Deeblock · Jul 11, 2018 at 05:56 AM
You can set each button to call an event pertaining to its own function, and output it to a log file (text, json, or xml whatever) in a location like persistantdatapath to access after the game is over.
For example, when you press the green button "go", under the button functions, you can call a function like "OutputDataLog()", which logs that the "go" button is pressed. The code would look something like:
public void OutputDataLog() // Each button will have their own function
{
// Change the string inside Add here to the corresponding button pressed
GameManager.Instance.gameData.Add("Go button pressed.")
}
// In your GameManager class
public List<String> gameData = new List<String>();
#region Singleton
// Singleton pattern
private static GameManager gameManagerInstance;
public static GameManager Instance { get { return gameManagerInstance; } }
private void Awake()
{
if (gameManagerInstance != null && gameManagerInstance != this)
{
if (gameObject != null)
{
Destroy(gameObject);
}
return;
}
gameManagerInstance = this;
DontDestroyOnLoad(gameObject);
if (gameData == null)
{
gameData = new List<String>();
}
}
#endregion
public void SaveDataLog()
{
if (!File.Exists(Application.persistentDataPath + "/log.txt"))
{
File.Create(Application.persistentDataPath + "/log.txt").Dispose();
}
string jsonData = JsonUtility.ToJson(gameData);
File.WriteAllText(Application.persistentDataPath + "/log.txt", jsonData);
}
public void LoadDataLog()
{
if (File.Exists(Application.persistentDataPath + "/log.txt"))
{
string jsonData = File.ReadAllText(path + saveName + ".dat");
gameData = new List<string>();
gameData = JsonUtility.FromJson<GameData>(jsonData);
}
else
{
Debug.LogError(Application.persistentDataPath + "/log.txt");
}
}
You can now simply call GameManager.Instance.gameData to access your log list in game.
Your answer
Follow this Question
Related Questions
Help with GUIStyle showing up 0 Answers
I dont understand why the script to read .xml doesnt works. 1 Answer
Display the same string in multiple text boxes 1 Answer
I have a problem with Unity UI Text 0 Answers
Get Text from an Input Field on one scene and use it as a text on another scene Unity c# 0 Answers