Page 1 of 2
Recording Variable of Answers as CSV
Posted: Mon Apr 09, 2018 8:05 am
by jibjab910
I am using Pixel Crushers to run an experiment in which I am logging the participants' responses as a string. However, my process for doing so is not working. I wanted to lay it out step-by-step to see where my issue is.
1. I am recording my users' answers using the script field in dialogue they select. It usually looks like this:
Code: Select all
Variable["EarthquakeAnswers"] = Variable["EarthquakeAnswers"] .. "1a,";
2. I run my own code called WriteCSV.cs, which looks like the following:
Code: Select all
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using PixelCrushers.DialogueSystem;
using UnityEngine;
public class WriteCSV : MonoBehaviour {
public void ExportAnswers ()
{
string strPath2 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory);
strPath2 = strPath2 + "/earthquake.txt";
FileStream fs2 = new FileStream(strPath2, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw2 = new StreamWriter(fs2);
sw2.BaseStream.Seek(0, SeekOrigin.End);
string answers2 = DialogueLua.GetVariable("EarthquakeAnswers").AsString;
sw2.WriteLine(answers2);
sw2.Flush();
sw2.Close();
}
}
3. My file is found on the desktop, but is blank.
Re: Recording Variable of Answers as CSV
Posted: Mon Apr 09, 2018 8:25 am
by jibjab910
Some more information: I have the database loaded in the scene in which my code is called, but it is not the same scene in which the variables are created/modified. Additionally, my project requires that the dialogue manager be destroyed on each scene load due to some problems with the UI.
Re: Recording Variable of Answers as CSV
Posted: Mon Apr 09, 2018 8:43 am
by Tony Li
Hi,
Is Variable["EarthquakeAnswers"] initialized? Make sure it's defined as a Text variable in your dialogue database.
As a debugging step, you might want to add this line temporarily:
Code: Select all
string answers2 = DialogueLua.GetVariable("EarthquakeAnswers").AsString;
Debug.Log(answers2);
This will tell you if the issue is with the "EarthquakeAnswers" variable or your file-writing code.
BTW, another way to do this (if you want to avoid setting the
Script field for every response) is to use the
OnConversationLine method. This is a special method that gets called on any script that's on the Dialogue Manager and the primary conversation participants. For example:
Code: Select all
void OnConversationLine(Subtitle subtitle) {
if (subtitle.speakerInfo.IsPlayer) {
Debug.Log("Player chose entry ID: " + subtitle.dialogueEntry.id);
}
}
Re: Recording Variable of Answers as CSV
Posted: Mon Apr 09, 2018 9:30 am
by jibjab910
Tony,
The variable is instantiated in the database. The code you gave me outputs the following:
Code: Select all
UnityEngine.Debug:Log(Object)
WriteCSV:ExportAnswers() (at Assets/_Scenes/Databases and animators/WriteCSV.cs:23)
UnityEngine.Events.UnityEvent:Invoke()
SelectionSliderButton:OnBarFilled() (at Assets/Dialogue System Examples/VR UI Example/Scripts/SelectionSliderButton.cs:28)
VRStandardAssets.Utils.<FillBar>c__Iterator1:MoveNext() (at Assets/VRSampleScenes/Scripts/Utils/SelectionSlider.cs:133)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
Unfortunately, I'm not sure what this entails. The the 22nd and 23rd lines are the ones you gave me:
Code: Select all
string answers3 = DialogueLua.GetVariable("EarthquakeAnswers").AsString;
Debug.Log(answers3);
Thank you for all the help. I am not a coder, but have been placed with this project nonetheless. You've been a great help.
Re: Recording Variable of Answers as CSV
Posted: Mon Apr 09, 2018 10:40 am
by Tony Li
As another debugging step, inspect a dialogue entry such as the one with this
Script:
Code: Select all
Variable["EarthquakeAnswers"] = Variable["EarthquakeAnswers"] .. "1a,";
Add a "ShowAlert" command:
Code: Select all
Variable["EarthquakeAnswers"] = Variable["EarthquakeAnswers"] .. "1a,";
ShowAlert("Current: " .. Variable["EarthquakeAnswers"])
This will show the value of Variable["EarthquakeAnswers"] as an onscreen alert.
Then play.
If you don't see an onscreen alert when selecting this answer, this dialogue entry isn't the one being chosen. Maybe you have a duplicate conversation and you're editing the other one accidentally.
If you see "Current:" without any text behind it (i.e.,
not "Current: 1a,") then this means the EarthquakeAnswers variable isn't initialized. Double-check that the variable in the dialogue database exactly matches "EarthquakeAnswers", including capitalization.
If none of that helps, please feel free to send a copy of your project to tony (at) pixelcrushers.com. I'll be happy to take a look.
Re: Recording Variable of Answers as CSV
Posted: Mon Apr 09, 2018 12:01 pm
by jibjab910
Dear Tony,
I see the alert with the exact value of the EarthquakeAnswers variable. I think the issue has to do with the way that my dialogue database is being re-loaded on every scene load. Is there some way I can write the current value of the answers variable to some global variable which can be accessed later? I'm checking that this is indeed the issue right now.
Re: Recording Variable of Answers as CSV
Posted: Mon Apr 09, 2018 12:52 pm
by jibjab910
Dear Tony,
The variable's value is being reset on every scene load. It is not being reset during a scene (I checked and the alert showed that the variable was being built out properly towards the end of a scene).
Re: Recording Variable of Answers as CSV
Posted: Mon Apr 09, 2018 1:22 pm
by Tony Li
Hi,
Can you keep the same Dialogue Manager across scenes instead of reloading the database in every scene? To do this, tick the Dialogue Manager's
Don't Destroy On Load and
Allow Only One Instance checkboxes.
If not, then you can save the variable by adding this script to each scene:
SaveAndLoadAcrossScenes.cs
Code: Select all
using UnityEngine;
using PixelCrushers.DialogueSystem;
public class SaveAndLoadAcrossScenes: MonoBehaviour
{
// Static variables exist globally:
public static string saveData = string.Empty;
// Before leaving a scene, store its save data.
void OnDestroy()
{
saveData = PersistentDataManager.GetSaveData();
}
// After entering a scene, load the saved data.
void Start()
{
if (!string.IsNullOrEmpty(saveData)) PersistentDataManager.ApplySaveData(saveData);
}
}
Re: Recording Variable of Answers as CSV
Posted: Mon Apr 09, 2018 1:35 pm
by jibjab910
Dear Tony,
I cannot, as it is necessary that I untick those boxes in order to get the VR dialgoue UI to work. Should I add this script to a game object and then use the sequence field to call the ondestroy and onstart commands?
Re: Recording Variable of Answers as CSV
Posted: Mon Apr 09, 2018 1:49 pm
by Tony Li
Just add the script to a GameObject in the scene. You don't need to call Start or OnDestroy. (I had a typo in the script. Use "Start", not "OnStart".) Unity will automatically call those methods.
It would be possible to get the VR dialogue UI to work with those checkboxes ticked, but if things are working properly now, then I think you shouldn't change it. If it isn't broken, don't fix it.
If you do want to change it at some point, just let me know.