Add trials during session - immersivecognition/unity-experiment-framework GitHub Wiki
You can add trials as a session is running ("on-the-fly") if this suits your experimental design. This can be used for staircase designs or similar. To do this, just create a trial on a desired block as needed. First in the below example, let's create some trials before our session starts, as normal.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// add the UXF namespace
using UXF;
public class SessionGenerator : MonoBehaviour
{
// assign this as the first element in the "On Session Begin" event in the Session component inspector
public void GenerateExperiment(Session session)
{
// create block of 20 trials
Block block1 = session.CreateBlock(20);
}
}
Elsewhere, we can create functionality captures participant responses, and stores them in trial.result. Then when the trial has ended, we can run a method to create a new trial if required, for example based on the trial response.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// add the UXF namespace
using UXF;
public class ExtraTrialCreator : MonoBehaviour
{
// assign this method in the "On Trial End" event in the Session component inspector
public void CreateAndRunNewTrialIfNeeded(Trial trial)
{
// example - create a new trial if participant answer is above 5
int answer = trial.result["answer"];
if (answer > 5)
{
// create new trial with "example" setting assigned to value 1234.
Trial newTrial = trial.block.CreateTrial();
newTrial.settings.SetValue("example", 1234);
// we could also directly start the newly created trial. this will skip past all the trials we had left to go.
newTrial.Begin();
}
else
{
// otherwise, we can end the session...
if (trial == trial.session.LastTrial)
{
trial.session.End();
}
// or start the next trial.
else
{
trial.session.NextTrial.Begin();
}
}
}
}