diff --git a/docs/scripting/syntax/return_object.mdx b/docs/scripting/syntax/return_object.mdx new file mode 100644 index 00000000..aab1d33f --- /dev/null +++ b/docs/scripting/syntax/return_object.mdx @@ -0,0 +1,42 @@ +--- +title: Return Object +alias: + - success object + - failure object +--- + +An object returned to the CLI from a script may be automatically formatted, depending on its contents. These types of objects are often referred to as ((%LSuccess%)) or ((%DFailure%)) objects. + +## Syntax + +A ((%LSuccess%)) or ((%DFailure%)) object can only contain two keys. The `ok` key is required, and determines whether ((%LSuccess%)) or ((%DFailure%)) is displayed in the terminal. You may also attach a `msg` key, which will append that value, after being converted to a string. + +## Example + +```js +function(context, args) { + //Explains the guessing game when the script is run without a guess. + if (!args.guess) { + return "Let's play a guessing game! Guess a number between 1 and 10!" + } + + //Return a success object if they guess they right number. + if (args.guess == Math.floor(Math.random() * 10)) { + return { + ok: true, + msg: args.guess + " was the right number!" + } + } + + //In all other cases, return a failure object. + return { + ok: false, + msg: "Sorry, please guess again!" + } +} +``` + +## See Also + +- [[ok]] +- [[not_impl]]