Skip to content
jackbrookes edited this page Sep 12, 2018 ยท 20 revisions

The settings system allows researchers to assign parameters (e.g. independent variables) to the Session, a Block, or a Trial. The settings system is handled with a dedicated class: Settings.

Instances of a Session, Block, or Trial each contain a .settings field which is initially populated with an empty settings object.

When we begin the Session, we optionally pass a Settings object to be then used as the settings for the Session.

Settings from .json files

By default the Session is started using the UI, with a selected .json file ("Experiment profile") is deserialized. The deserialisation is performed by the popular MiniJSON script. When deserializing from .json, care must be taken when converting the type of the objects in our settings file.

The JSON is just a string - so MiniJSON interprets each value and attempts to deserialize it into an appropriate C# type.

Example JSON JSON type C# cast example
{"example": "hello"} string (string) settings["example"]
{"example": 123} int (long) settings["example"]
{"example": 3.14} float (double) settings["example"]
{"example": [1, 2, 3]} array (List<object>) settings["example"]
{"example": {"a": 1, "b": "hello"}} Dictionary<string, object> (Dictionary<string, object>) settings["example"]
{"example": true} bool (bool) settings["example"]

There are also ways to convert objects more directly to useful types found in unity (i.e. float and int). These are also less error prone - e.g. trying to deserialize { "example": 1 } with (long) settings["example"] would fail.

Note: Add using System; to the top of your script to gain access to the Convert.To*() methods.

Example JSON JSON type Desired C# type C# Conversion example
{"example": 123} int int Convert.ToInt32(settings["example"])
{"example": 123} int float Convert.ToSingle(settings["example"])
{"example": 3.14} float float Convert.ToSingle(settings["example"])

If you have issues you can check validity of your JSON files with an online validator tool.

Cascading requests

The setting system is set up such that, if a settings is unavailable, the request will cascade up the experiment hierarchy. For example, if a settings is not available for the Trial, it will look inside the Block, and then inside the Session.

๐Ÿง  Core topics

โ“ More help


๐Ÿ‘ฉโ€๐Ÿ’ป Programming reference

Unit tests

Clone this wiki locally