The SDK of this version is still being tested, and these pages are being updated with it: names, menus and procedures may still change before the release. If you build content for a platform already in production, follow the documentation of that version — pick it from the version menu at the top right.
Tasks API
ITasksApi
The Tasks API lets an interpreted script read and drive the tasks of the scene: read a task's state, complete it with a result, report an attempt, fail it, unlock it, ask for a hint, and hear about every change. It is reached from the scripting API:
ITasksApi tasks = IVirtuademyGameplay.Current.Tasks; // namespace Virtuademy.Environments.ScriptingApi
Types and delegates are in the namespace Virtuademy.Environments.ScriptingApi.Tasks. Everything is called on the main thread.
A task is identified by its GameObject: the object that carries the Task component (the node of the task graph). Reference it from your script with a GameObject field set in the inspector. Passing anything else logs one warning and does nothing.
A script cannot call Task.CompleteTask() itself: Task derives from a type of the graph package that the script's assembly does not reference, so the compiler refuses it (error CS0012). The API takes the object instead.
Members
| Member | What it does | Node |
|---|---|---|
bool IsAvailable | Whether the world has at least one task system awake | |
ETaskState GetTaskState(GameObject task) | The state of the task | Get Task State |
void CompleteTask(GameObject task) | Completes the task when it is to do, as a right result. It raises no result event | Complete Task |
void CompleteTask(GameObject task, TaskResult result) | Completes the task with a result. A procedure scores it | Complete Task With Result |
void ReportAttempt(GameObject task, TaskResult result) | Reports an attempt that does not complete the task. A wrong one is an error of the procedure | Report Attempt |
void FailTask(GameObject task, string reason) | Fails the task, with a reason for the summary | Fail Task |
void UnlockTask(GameObject task) | Unlocks the task: it becomes to do | Unlock Task |
void RequestHint(GameObject procedure) | Shows the hint of the current task of the procedure on that object (or on its task system's object) | Request Hint |
event TaskStateChangedHandler TaskStateChanged | A task of any task system changed state | On Task Unlocked, On Task Completed, On Task Failed (each for one task) |
event TaskResultHandler TaskResultReported | A result was reported on a task (attempts and completions) | On Task Attempt |
The nodes are in Task System nodes and Procedure nodes.
What a report does depends on the task:
- A report on a task that is completed or failed is ignored (it is logged once for the task). A report on a locked task is ignored too, unless the procedure's Out Of Order Is Error is on: it is then a wrong attempt.
- Without a procedure,
CompleteTask(task, result)completes the task and the flow goes on, andReportAttemptonly raises the event. - With a procedure, the results are scored: see Procedure.
Results
A TaskResult says how an action went.
TaskResult right = TaskResult.Right(gameObject); // outcome Right, score 1
TaskResult wrong = TaskResult.Wrong(gameObject, "Valve was closed"); // outcome Wrong, score 0, with a reason
TaskResult partial = new TaskResult(ETaskOutcome.Partial); // score 0.5
TaskResult custom = new TaskResult(ETaskOutcome.Partial, 0.8f, gameObject, "Slow", "answer=B");
| Member | Meaning |
|---|---|
Outcome (ETaskOutcome) | Right, Wrong or Partial |
Score | From 0 to 1. The outcome's own when you give none: right 1, wrong 0, partial 0.5. Values outside 0 to 1 are clamped |
Source | The object that produced it (the object acted on, an element); may be null |
Reason | Why, in a few words. It is the wrong feedback when the task has none, and a row of the summary's timeline |
Data | Anything you want to keep with the result (the answer chosen, say) |
IsLocalActor | Whether this client's user produced it. False for a result received from another user of a shared procedure |
ActorName | The name of that other user; empty for this user's own results |
IsLocalActor and ActorName are set by the task system: you read them, you do not set them.
ETaskState is the state of a task: Locked (not reachable yet), Todo (can be done now), InProgress (a task with steps of its own, started), Completed, Failed.
Events
The events use named delegates, so an interpreted script can subscribe to them. Write the handler as a method and subscribe it as a method group, not as Action<...>:
public delegate void TaskResultHandler(GameObject task, TaskResult result);
public delegate void TaskStateChangedHandler(GameObject task, ETaskState previous, ETaskState current);
TaskResultReported is raised for every result reported, on this client or received from another user of a shared procedure, with the task's object.
Example
A valve that completes its task when it is turned at the right pressure, and counts a wrong attempt when it is not:
using UnityEngine;
using Virtuademy.Environments.ScriptingApi;
using Virtuademy.Environments.ScriptingApi.Tasks;
public class PressureValve : MonoBehaviour
{
[SerializeField] private GameObject task; // the object with the Task of this step
[SerializeField] private float rightPressure = 3f;
// Hook this to the Interactable's On Interact, or call it from your own logic.
public void Turn(float pressure)
{
if (!IVirtuademyGameplay.IsAvailable)
{
return;
}
ITasksApi tasks = IVirtuademyGameplay.Current.Tasks;
if (tasks.GetTaskState(task) != ETaskState.Todo)
{
return;
}
if (Mathf.Abs(pressure - rightPressure) < 0.1f)
{
tasks.CompleteTask(task, TaskResult.Right(gameObject));
}
else
{
tasks.ReportAttempt(task, TaskResult.Wrong(gameObject, "Wrong pressure"));
}
}
}
To react to results, subscribe in Start and unsubscribe in OnDestroy:
private void Start()
{
if (IVirtuademyGameplay.IsAvailable)
{
IVirtuademyGameplay.Current.Tasks.TaskResultReported += OnResult;
}
}
private void OnDestroy()
{
if (IVirtuademyGameplay.IsAvailable)
{
IVirtuademyGameplay.Current.Tasks.TaskResultReported -= OnResult;
}
}
private void OnResult(GameObject reportedTask, TaskResult result)
{
if (reportedTask == task && result.Outcome == ETaskOutcome.Wrong)
{
Debug.Log("Wrong: " + result.Reason);
}
}
What not to do
- Do not call the API before the task system is up.
IsAvailableis false until a task system has woken up: check it, or wait for the On Task System Ready node. - Do not report the same action from every client of a shared procedure. A report made on a client is applied there and sent to the room, so a wrong attempt reported by every client counts once for each. Report from the client of the user who acted, or from the master.
- Do not use
CompleteTask(task)when you want the result scored. It completes the task as a right result and raises noTaskResultReported: useCompleteTask(task, result). - Do not use
Action<T>for the handlers. Use the named delegates, as shown.
See also Writing scripts for the rest of the scripting API.