Skip to content

Adding OnBlockBegin and OnBlockEnd UnityEvents #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Assets/UXF/Scripts/Etc/Editor/SessionEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,23 @@ protected override void DrawInspector()

EditorGUILayout.Space();
EditorGUILayout.HelpBox(GetTooltip(session.GetType().GetField("onSessionBegin")).tooltip, MessageType.Info);
DrawPropertiesFromUpTo("onSessionBegin", "onTrialBegin");
DrawPropertiesFromUpTo("onSessionBegin", "onBlockBegin");

EditorGUILayout.Space();
EditorGUILayout.HelpBox(GetTooltip(session.GetType().GetField("onBlockBegin")).tooltip, MessageType.Info);
DrawPropertiesFromUpTo("onBlockBegin", "onTrialBegin");

EditorGUILayout.Space();
EditorGUILayout.HelpBox(GetTooltip(session.GetType().GetField("onTrialBegin")).tooltip, MessageType.Info);
DrawPropertiesFromUpTo("onTrialBegin", "onTrialEnd");

EditorGUILayout.Space();
EditorGUILayout.HelpBox(GetTooltip(session.GetType().GetField("onTrialEnd")).tooltip, MessageType.Info);
DrawPropertiesFromUpTo("onTrialEnd", "preSessionEnd");
DrawPropertiesFromUpTo("onTrialEnd", "onBlockEnd");

EditorGUILayout.Space();
EditorGUILayout.HelpBox(GetTooltip(session.GetType().GetField("onBlockEnd")).tooltip, MessageType.Info);
DrawPropertiesFromUpTo("onBlockEnd", "preSessionEnd");

EditorGUILayout.Space();
EditorGUILayout.HelpBox(GetTooltip(session.GetType().GetField("preSessionEnd")).tooltip, MessageType.Info);
Expand Down
9 changes: 9 additions & 0 deletions Assets/UXF/Scripts/Etc/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ public class SessionEvent : UnityEvent<Session>

}

/// <summary>
/// Event containing a Block as a parameter
/// </summary>
[Serializable]
public class BlockEvent : UnityEvent<Block>
{

}

/// <summary>
/// Event containing a Trial as a parameter
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions Assets/UXF/Scripts/Etc/Trial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ public void Begin()
Utilities.UXFDebugLogWarning("An item in the Tracked Objects field of the UXF session if empty (null)!");
}
}

if (this == block.firstTrial)
{
session.onBlockBegin.Invoke(block);
}

session.onTrialBegin.Invoke(this);
}

Expand All @@ -137,6 +143,11 @@ public void End()
}

session.onTrialEnd.Invoke(this);

if (this == block.lastTrial)
{
session.onBlockEnd.Invoke(block);
}
}

public bool CheckDataTypeIsValid(string dataName, UXFDataType dataType)
Expand Down
22 changes: 19 additions & 3 deletions Assets/UXF/Scripts/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ public class Session : MonoBehaviour, IExperimentUnit, IDataAssociatable
[Tooltip("Items in this event will be triggered when the session begins. Useful generating your trials & blocks, setting up the scene, and triggering the first trial.")]
public SessionEvent onSessionBegin = new SessionEvent();

/// <summary>
/// Event(s) to trigger when a block begins. Can pass the instance of the Block as a dynamic argument
/// </summary>
/// <returns></returns>
[Tooltip("Items in this event will be triggered each time a block begins.")]
public BlockEvent onBlockBegin = new BlockEvent();

/// <summary>
/// Event(s) to trigger when a trial begins. Can pass the instance of the Trial as a dynamic argument
/// </summary>
Expand All @@ -107,6 +114,13 @@ public class Session : MonoBehaviour, IExperimentUnit, IDataAssociatable
[Tooltip("Items in this event will be triggered each time a trial ends. Useful for collecting results from the trial as well as showing feedback.")]
public TrialEvent onTrialEnd = new TrialEvent();

/// <summary>
/// Event(s) to trigger when a block ends. Can pass the instance of the Block as a dynamic argument
/// </summary>
/// <returns></returns>
[Tooltip("Items in this event will be triggered each time a block ends.")]
public BlockEvent onBlockEnd = new BlockEvent();

/// <summary>
/// Event(s) to trigger just before the session has ended. If you wish to perform any summary statistics or write any final session data this is the time to do it. Do not use this event to quit the application.
/// </summary>
Expand Down Expand Up @@ -486,15 +500,17 @@ Trial GetFirstTrial()
Trial GetLastTrial()
{
if (blocks.Count == 0) throw new NoSuchTrialException("There is no last trial because no blocks have been created!");

Block lastValidBlock;
Trial lastTrial;
int i = blocks.Count - 1;
while (i >= 0)
{
lastValidBlock = blocks[i];
if (lastValidBlock.trials.Count > 0)
lastTrial = lastValidBlock.lastTrial;
if (lastTrial != null)
{
return lastValidBlock.trials[lastValidBlock.trials.Count - 1];
return lastTrial;
}
i--;
}
Expand Down