Skip to main content

Parameterized Example

It's up to you to decide what aspects of an assessment should be configurable parameters. As an example, we'll extend a previous example and make the following into parameters:

  • The number of trials.
  • The button labels for the affirmative and negative responses.

Define the parameters with JSON Schema

const defaultParameters = {
number_of_trials: {
type: "integer",
default: 3,
description: "How many trials to run.",
},
affirmative_text: {
type: "string",
default: "Yep!",
description: "Text for the affirmative button.",
},
negative_text: {
type: "string",
default: "Nope!",
description: "Text for the negative button.",
},
};

We've named the variable for the parameters object as defaultParameters. You can name it anything you like, as long as you use the same name in the parameters property of the game options.

Assign the parameters in the game options

const options = {
name: "Documentation Example",
id: "docs",
publishUuid: "c9ec7b5b-a6cc-4308-9b1c-73b40ae4aa9e",
width: 200, height: 400,
fonts: [{
fontName: "roboto",
url: "fonts/roboto/Roboto-Regular.ttf"
}],
trialSchema: demoSchema,
parameters: defaultParameters
};

Use Parameters in the Assessment

The code gets the "Yes" button text from the parameter affirmative_text, not a hard-coded value:

const yesButton = new Button({
text: game.getParameter("affirmative_text"),
size: { width: 75, height: 50 },
position: { x: 50, y: 200 },
isUserInteractionEnabled: true
})

The button handlers check if the game's trial index is at the limit, number_of_trials, and if so, ends the assessment by advancing to an ending scene:

yesButton.onTapDown(() => {
game.addTrialData("mobile_research_fun", true);
game.addTrialData("trial_index", game.trialIndex);
game.trialComplete();
if (game.trialIndex === game.getParameter("number_of_trials")) {
game.presentScene(endScene);
}
});

Try changing the default value of affirmative_text from Yep! to Yes, click "Run", and see how the button text changes.

Loading...
Explore:
  • You will get an error if you try to get a game parameter that was not defined in the schema. For example, this example incorrectly tries to get the number of trials from a non-existent parameter called maximum_trials. It will throw an error when you click a response.