A package puts settings and buttons on a bot's page by declaring them in its script. The complete schema is below. Declare settings and Declare actions walk through it with examples, and asterobot:parameters describes how a script reads the values.
Where declarations go
A package declares settings and actions as two named exports of its index.js:
export const parameters = {
maxFights: { type: "int", label: "Stop after", default: 20, min: 1 },
};
export const actions = {
recall: {
label: "Recall to zaap",
async run() {
// What the button does.
},
},
};
- Both exports are optional. Exports with these names in other files of the package are ignored.
- Every package of the running script can declare its own: the bot's package, and each dependency the script really imports. A dependency listed in
asterobot.jsonthat nothing imports declares nothing. - Settings and actions appear on the bot's page in the order you write them.
- These are ordinary JavaScript values, so a default can be computed or imported from another file.
How Asterobot reads them
Asterobot runs the top level of the package's modules without calling the default export, then reads the two exports. It does so when a bot's Settings tab shows Package settings, when you pick the package in the Add a bot dialog, and each time a script starts.
At that moment there's no bot and no game session, so some functions behave differently when the top level of a module calls them:
| Called at the top level | While Asterobot reads declarations |
|---|---|
send(), request(), wait(), sleep(), disconnect(), currentMap(), and every function of asterobot:gamedata and asterobot:pathfinding |
Throws, and reading fails with an error that ends with not available while reading a package's declarations |
move() |
Rejects with an error that ends with not available while reading a package's declarations |
on(), onTraffic(), intercept(), off(), onChange(), offChange() |
Accepted, and does nothing |
session |
Holds empty values |
values |
An empty object |
botDebug() and the other console functions of asterobot:bot and asterobot:console |
Write nothing |
packages.info() |
Answers as usual |
Reading also fails when a top-level await never settles, with package awaits at import time, which cannot be answered without a Game session, and when top-level code runs for more than 250 ms without awaiting, with JavaScript execution deadline exceeded. A rejected promise that nothing catches doesn't make reading fail.
So keep the top level of your modules to imports, declarations and quick computations.
Settings
export const parameters is an object. Each key is a setting's name, the name the script reads in values, and each value is an object with these fields:
| Field | Type | Required | Description |
|---|---|---|---|
type |
string | Yes | "bool", "string", "int", "double", "array" or "map" |
of |
string | For array and map only |
The type of the array's items, or of the map's values: "bool", "string", "int" or "double". Refused on the other types. |
label |
string | No | Shown next to the input. Default: the setting's name. |
description |
string | No | A line of help shown with the input |
placeholder |
string | No | Shown in an empty text box or number box |
unit |
string | No | A short word shown after a number box, such as kamas or seconds |
default |
depends on type |
No | The value until someone changes it. Without a default, or with null, the setting starts at its type's zero value. |
choices |
array | No | For string and int only: the values allowed. See Choices. |
min |
number | No | The smallest value allowed, for int and double |
max |
number | No | The largest value allowed, for int and double |
Any other field is ignored without an error, so a misspelled field such as labell silently does nothing. A label, description, placeholder or unit that isn't a string is ignored too, and so is a min or max that isn't a number.
Types
type |
The script reads | On the bot's page | Zero value |
|---|---|---|---|
bool |
A boolean | A switch | false |
string |
A string | A text box | "" |
int |
A whole number, never a BigInt | A number box that steps by 1 | 0 |
double |
A number | A number box | 0 |
array |
An array | A list with an input per item, a remove button on each, and Add | [] |
map |
An object with string keys | A list of key and value rows, with Add an entry. A row with an empty key is dropped. | {} |
A setting with choices shows a drop-down list, whatever its type, and its zero value is the first choice.
Values that fit
The default, and every value applied from the bot's page, has to fit the declaration. Asterobot never converts one type into another:
booltakestrueorfalse, nothing else.stringtakes a string. A number isn't turned into text.inttakes a whole, finite number.3.0is fine, and3.5is refused rather than rounded.doubletakes a finite number: notNaN, notInfinity.arraytakes an array whose every item fitsof.maptakes an object whose every value fitsof.- With
choices, the value has to be one of the choices. - With
minormax, anintordoublevalue has to be within the bounds, the bounds themselves included.minandmaxdon't apply to a setting that haschoices, nor to the items of anarrayor amap.
A saved value that stops fitting after you change a declaration is ignored, and the setting falls back to its default.
Choices
choices takes bare values, objects with a value and a label, or a mix of both. A bare value is its own label.
export const parameters = {
slot: { type: "int", label: "Slot", choices: [1, 2, 3], default: 2 },
mode: {
type: "string",
label: "Strategy",
choices: [
{ value: "safe", label: "Avoid groups" },
{ value: "greedy", label: "Fight everything" },
],
default: "safe",
},
};
Every choice's value has to fit the setting's type, and a default has to be one of the choices.
Actions
export const actions is an object. Each key is an action's name, and each value is an object with these fields:
| Field | Type | Required | Description |
|---|---|---|---|
label |
string | No | The button's text. Default: the action's name. |
description |
string | No | Shown as the button's tooltip, and at the top of the arguments dialog |
args |
object | No | The arguments the bot's page asks for before running the action. Each one is declared exactly like a setting, with the same fields and rules, but its value isn't saved. |
run |
function | Yes | What the button does |
run(args) receives an object holding every declared argument: the value typed in the dialog, or the argument's default when none was given. An action without args receives {}. run may be async.
import { send } from "asterobot:protocol";
export const actions = {
say: {
label: "Say something",
args: { text: { type: "string", label: "Text", default: "Hello" } },
async run({ text }) {
await send("ChatChannelMessageRequest", { channel: "GLOBAL", content: text });
},
},
};
Here is what happens around an action:
- Its button is in the package's section of Package settings, on the bot's Settings tab. It can only be clicked while a script is running. Otherwise it's disabled, with the tooltip
The bot has to be playing a behavior before an action can run. - An action with arguments opens a dialog titled with its label, with one field per argument, reset to the defaults every time it opens, and Cancel and Run buttons.
- The arguments are checked against the declarations before anything runs. A value that doesn't fit shows Couldn't start the action with the reason, and
runisn't called. - Once the action is on its way, a toast says Action started. When
runreturns, or its promise settles, a second toast says the action's name followed byfinished, or followed byfailedwith the error. A failure toast stays until you close it. - An error in
runnever stops the script. - Asterobot finds the actions before it calls the default export, so the buttons work even when the entry function never finishes.
- A button runs the action of the script that's running. After you change an action in the editor, start the script again to use the new code.
Types for the editor
The editor knows these types for declarations: BehaviorParameters, BehaviorActions, BehaviorParameter, BehaviorAction, BehaviorChoice, BehaviorParameterType and BehaviorScalarType. Name them in a JSDoc comment to get completion on the fields:
/** @type {BehaviorParameters} */
export const parameters = {
enabled: { type: "bool", label: "Answer in chat", default: true },
};
Errors
When declarations can't be read, Package settings on the bot's Settings tab, and the package's settings in the Add a bot dialog, show Couldn't read this package's settings with the reason. The text starts with the package's name and version and read declarations:, such as:
my-first-script@1.0.0: read declarations: package "my-first-script" parameter "enabled": parameter "enabled" default: want a bool, got string
In the texts below, <package> is the package's name, <name> a setting's name, <action> an action's name and <argument> an argument's name.
Errors that also stop the script from starting
These say an export doesn't have the right shape. They also make every start fail, with the Problems alert showing read declarations of "<module>": followed by the same text.
| Error | Cause |
|---|---|
package "<package>": parameters: must be an object, got <type> |
parameters is exported, but isn't an object |
package "<package>": parameters: "<name>" must be an object |
A setting isn't an object |
package "<package>": actions: must be an object, got <type> |
actions is exported, but isn't an object |
package "<package>": actions: "<action>" must be an object |
An action isn't an object |
package "<package>": actions: "<action>" declares no run function |
An action has no run |
package "<package>": actions: "<action>": run must be a function |
An action's run isn't a function |
package "<package>": actions: "<action>": args: must be an object, got <type> |
args isn't an object |
package "<package>": actions: "<action>": args: "<argument>" must be an object |
An argument isn't an object |
Errors in a setting or an argument
These don't stop a start: the script starts, but with no settings at all. They begin with package "<package>" parameter "<name>": for a setting, or with package "<package>" action "<action>": argument "<argument>": for an action's argument.
| Error | Cause |
|---|---|
parameter "<name>" declares unknown type "<type>" |
type is missing or isn't one of the six types |
parameter "<name>" is a <type> and must declare its element type as `of` |
An array or map without of |
parameter "<name>" declares element type "<of>", which is not one of bool/string/int/double |
of isn't one of the four scalar types |
parameter "<name>" is a <type> and cannot declare an element type |
of on a type other than array and map |
parameter "<name>" declares choices, which only a string or an int may do |
choices on a type other than string and int |
choices must be an array, got <type> |
choices isn't an array |
choice <index> has no value |
A choice object without value. Choices count from 0. |
parameter "<name>" choice <index>: <reason> |
A choice's value doesn't fit the type |
parameter "<name>" declares min <min> above max <max> |
min is greater than max |
parameter "<name>" default: <reason> |
The default doesn't fit the declaration |
parameter has no name |
A setting's name is the empty string |
Reasons a value doesn't fit
These reasons follow default: or choice <index>: in the errors above. The same texts appear when a value applied from the bot's page, or an action's argument, doesn't fit.
| Reason | Cause |
|---|---|
want a bool, got <type> |
A bool got something other than true or false |
want a string, got <type> |
A string got something other than a string |
want an int, got <type> |
An int got something other than a number |
want a whole number, got <value> |
An int got a fraction, or Infinity or NaN |
want a number, got <type> |
A double got something other than a number |
want a finite number, got <value> |
A double got NaN or Infinity |
want an array, got <type> |
An array got something other than an array |
item <index>: <reason> |
An array item doesn't fit of |
want a map, got <type> |
A map got something other than an object |
key "<key>": <reason> |
A map value doesn't fit of |
<value> is not one of <choices> |
The value isn't among the choices, listed with commas |
<value> is below the minimum <min> |
The value is lower than min |
<value> is above the maximum <max> |
The value is higher than max |
<type> is the name Asterobot gives the value's type: string, bool, int64 for a whole number written in the script, float64 for other numbers and for every number sent from the bot's page, []interface {} for an array and map[string]interface {} for an object.
Error messages lists the texts you can meet everywhere else.
Aucun avis à afficher.