Sample Javascript UDF: Difference between revisions
Jump to navigation
Jump to search
(Created page with "{{Languages|Javascript}} This is a very simple side-initiative tracker. It supports two teams, lets you mark one groups of tokens as the player team, the other as the opponen...") |
m (Replaced source tages with syntaxhighlight) |
||
Line 3: | Line 3: | ||
< | <syntaxhighlight lang="js"> | ||
let team = [] | let team = [] | ||
let opponents = [] | let opponents = [] | ||
Line 61: | Line 61: | ||
</ | </syntaxhighlight> | ||
And a related library that uses the same team concept, so uses the same namespace. | And a related library that uses the same team concept, so uses the same namespace. | ||
< | <syntaxhighlight lang="js"> | ||
function sendToMap(tokens) { | function sendToMap(tokens) { | ||
let maps = JSON.parse(MTScript.evalMacro("[r: getAllMapNames('json')]")) | let maps = JSON.parse(MTScript.evalMacro("[r: getAllMapNames('json')]")) | ||
Line 89: | Line 89: | ||
MTScript.registerMacro("sendSelectedToMap", sendSelectedToMap); | MTScript.registerMacro("sendSelectedToMap", sendSelectedToMap); | ||
</ | </syntaxhighlight> | ||
These functions are all then available in MTScript via {{code|[r: js.<functionName>]}}. | These functions are all then available in MTScript via {{code|[r: js.<functionName>]}}. |
Revision as of 18:12, 14 July 2022
Languages: English
This is a very simple side-initiative tracker. It supports two teams, lets you mark one groups of tokens as the player team, the other as the opponent team, then set or clear a state on the whole side at once. Additionally, there is a function for teleporting the team to a map, complete with a list box of maps.
let team = []
let opponents = []
let currentTeam = true;
function setTeam() {
team = []
for (let token of MapTool.getSelectedTokens()) {
team.push(token);
}
}
function setOpponents() {
opponents = []
for (let token of MapTool.getSelectedTokens()) {
opponents.push(token);
}
}
function actionDone() {
let tokens = MapTool.getSelectedTokens()
for (let token of tokens) {
MTScript.setVariable("tokenID", token.getId())
MTScript.evalMacro('[r: setState("Ready", 0, tokenID)]')
}
}
function sideDone() {
currentTeam = !currentTeam
let side1;
let side2;
if (currentTeam) {
side1 = team;
side2 = opponents
}
else {
side1 = opponents;
side2 = team;
}
for (let token of side1) {
MTScript.setVariable("tokenID", token.getId())
MTScript.evalMacro('[r: setState("Ready", 0, tokenID)]')
}
for (let token of side2) {
MTScript.setVariable("tokenID", token.getId());
MTScript.evalMacro('[r: setState("Ready", 1, tokenID)]');
}
}
MTScript.registerMacro("setTeam", setTeam);
MTScript.registerMacro("setOpponents", setOpponents);
MTScript.registerMacro("sideDone", sideDone);
MTScript.registerMacro("actionDone", actionDone);
And a related library that uses the same team concept, so uses the same namespace.
function sendToMap(tokens) {
let maps = JSON.parse(MTScript.evalMacro("[r: getAllMapNames('json')]"))
let listOption = `targetMap|${maps.join()}|Destination|LIST|SELECT=0 VALUE=STRING`
MTScript.setVariable("listOption", listOption)
MTScript.evalMacro("[r: input(listOption)]")
for (let token of tokens) {
MTScript.setVariable("tokenID", token.getId());
MTScript.evalMacro("[r: moveTokenToMap(tokenID, targetMap)]")
}
}
function sendTeamToMap() {
sendToMap(team)
}
function sendSelectedToMap() {
sendToMap(MapTool.getSelectedTokens())
}
MTScript.registerMacro("sendTeamToMap", sendTeamToMap);
MTScript.registerMacro("sendSelectedToMap", sendSelectedToMap);
These functions are all then available in MTScript via [r: js.<functionName>]
.